0

Есть такая разметка:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".pollsModule.PollsScr">


    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginTop="3dp"
        android:background="@color/colorAccent" />


    <TextView
        android:id="@+id/intro_text_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="10dp"
        android:textAlignment="center"
        android:visibility="gone" />

    <TextView
        android:id="@+id/intro_text_body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/intro_text_header"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:visibility="gone" />

    <Button
        android:id="@+id/next"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/main_screen_btn"
        android:visibility="gone" />


    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:layout_below="@id/divider">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/question_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginStart="10dp"
                android:layout_marginTop="15dp"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="10dp"
                android:fontFamily="@font/opensans_semibold"
                android:textColor="#666666"
                android:visibility="gone"
                app:autoSizeMaxTextSize="18sp"
                app:autoSizeMinTextSize="14sp"
                app:autoSizeStepGranularity="1sp"
                app:autoSizeTextType="uniform" />

            <EditText
                android:id="@+id/user_input"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/question_text"
                android:layout_marginStart="10dp"
                android:layout_marginTop="15dp"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="10dp"
                android:fontFamily="@font/opensans_semibold"
                android:textColor="#666666"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/questionImg"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:contentDescription="QuestionImg"
                android:visibility="gone" />


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/answerOptions"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/user_input"
                android:layout_marginStart="5dp"
                android:layout_marginTop="2dp"
                android:layout_marginEnd="5dp"
                android:layout_marginBottom="10dp"
                android:overScrollMode="never"
                android:visibility="gone"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

            <LinearLayout
                android:id="@+id/question_buttons"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/answerOptions"
                android:orientation="horizontal"
                android:visibility="gone"
                android:weightSum="20">

                <Button
                    android:id="@+id/prev_question"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_weight="10"
                    android:text="@string/previous_question_polls" />

                <Button
                    android:id="@+id/next_question"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_weight="10"
                    android:background="@drawable/main_screen_btn"
                    android:text="@string/next_question_polls"
                    android:visibility="gone" />

            </LinearLayout>
        </RelativeLayout>

    </androidx.core.widget.NestedScrollView>
</RelativeLayout>

и там есть такой список:

 <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/answerOptions"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/user_input"
                android:layout_marginStart="5dp"
                android:layout_marginTop="2dp"
                android:layout_marginEnd="5dp"
                android:layout_marginBottom="10dp"
                android:overScrollMode="never"
                android:visibility="gone"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

В этом списке при определенных условиях может быть такая разметка у элемента:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/question_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:textColor="#666666"
        app:autoSizeMaxTextSize="16sp"
        app:autoSizeMinTextSize="12sp"
        app:autoSizeStepGranularity="1sp"
        app:autoSizeTextType="uniform" />


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/answerOptions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/question_text"
        android:layout_marginStart="5dp"
        android:layout_marginTop="2dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="10dp"
        android:overScrollMode="never"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</RelativeLayout>

Если проще говоря, то будет несколько списков внутри одного списка:

NestedScrollView
- RecyclerView_1
-- LinearLayout
--- RecyclerView_2

Не уверен что правильно изобразил, но я постарался изобразить наглядно)) Суть проблемы: например в списке RecyclerView_1 3 элемента а в каждом из 3 списков RecyclerView_2 есть по 6 элементов. Если мы нажимаем например на один из элементов RecyclerView_2, который находится на позиции 2 в RecyclerView_1 то или мой NestedScrollView или RecyclerView_1 скроллится к самому верху. Не могу понять в чем может быть проблема. Думал сделать smoothScrollTo но не помогло.

4
  • А разве использование RecylerView в NestedScrollView не приводит к утечкам памяти? Даже при малом количестве элементов списка на старых устройствах приложение тормозило по полной программе, особенно когда элементы сложные. 31 янв 2020 в 1:21
  • @AndreyMihalev, вроде эмулятор без фризов работал, на реальных устройствах не пробовал еще. Это считается сложный элемент? Просто второй вариант - это динамическое создание такой разметки и других вариантов у меня нету
    – Andrew
    31 янв 2020 в 5:17
  • В зависимости от того, сколько у вас там элементов в списке. Возможно проблем не будет. Просто учтите, что RecylerView обернутый в NestedScrollView заставляет первый вывести все элементы в память, минуя кэширование. Посмотрите в профилировании использование ОЗУ на устройстве. Если для вас это не критично, то конечно такая реализация имеет место быть. 31 янв 2020 в 5:59
  • @AndreyMihalev, это единственное что пришло мне на ум по поводу реализации))) другие варианты сложнее будет сделать и выхлоп не очень большой будет. в целом спасибо за полезные советы, нужно будет проверить :)
    – Andrew
    31 янв 2020 в 6:10

1 ответ 1

0

Неожиданно нашел вроде решение, когда правильно сформулировал запрос в гугл)) Нужно добавить в RelativeLayout который находится внутри NestedScrollView такую строку:

android:descendantFocusability="blocksDescendants"

Вот где нашел это решение.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.