Странное поведение recyclerview в вложенном прокрутке

У меня есть макет, как показано ниже. Проблема в том, что высота recyclerview показывает только один элемент.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent">

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:orientation = "vertical"
        android:layout_margin = "@dimen/activity_horizontal_margin">

        <LinearLayout
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            android:orientation = "horizontal">

            <TextView
                android:layout_width = "0dp"
                android:layout_height = "wrap_content"
                android:layout_weight = "1"
                android:text = "Live Transactions"
                android:textStyle = "bold"/>

            <ImageButton
                android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                app:srcCompat = "@drawable/ic_refresh"
                android:background = "@android:color/transparent"/>

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id = "@+id/recycler_view"
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

Странно то, что когда я меняю атрибут

app:srcCompat = "@drawable/ic_refresh"

в ImageButton что-то вроде

app:srcCompat = "@android:drawable/ic_menu_search"

высота recyclerview становится нормальной, отображается большинство элементов. ImageButton находится над RecyclerView. Почему это происходит?

Попробуйте добавить recyclerView.setNestedScrollingEnabled(true/false)

Shrey Garg 01.05.2019 12:16

Это не работает

nwagu 01.05.2019 13:49
0
2
96
3
Перейти к ответу Данный вопрос помечен как решенный

Ответы 3

Вы должны сделать свой внутренний линейный макет соответствующим родительскому

<android.support.v4.widget.NestedScrollView xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent">

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:orientation = "vertical"
        android:layout_margin = "16dp">

        <LinearLayout
            android:layout_width = "match_parent"
            android:layout_height = "match_parent"
            android:orientation = "horizontal">

            <TextView
                android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                android:text = "Live Transactions"
                android:textStyle = "bold"/>

            <ImageButton
                android:layout_width = "wrap_content"
                android:layout_height = "wrap_content"
                app:srcCompat = "@mipmap/ic_launcher"
                android:background = "@android:color/transparent"/>

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id = "@+id/recycler_view"
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

Создание соответствия LinearLayouts родителю не работает. Хотя использование мипмапа работает. Но я хочу использовать кнопку рисования в кнопке изображения

nwagu 01.05.2019 15:16

Реальный вопрос заключается в том, почему это имеет значение для recyclerview, если я использую свой drawable против, если я использую ресурс Android или mipmap

nwagu 01.05.2019 15:17

В приведенном выше коде с внутренним линейным макетом в качестве родительского элемента вы можете использовать сам drawable. Не делайте только линейный макет, поскольку он соответствует родителю, также я удалил вес из текстового представления.

Developer 03.05.2019 08:51

Сделайте layout_height в представлении Recycler View равным match_parent

<android.support.v7.widget.RecyclerView
    android:id = "@+id/recycler_view"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent" />
Ответ принят как подходящий

Решил просто добавив

android:fillViewport = "true"

в NestedScrollView

Другие вопросы по теме