0

I have a layout like below. Problem is that the recyclerview height only shows one item.

<?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>

Strange thing is that when I change the attribute

app:srcCompat="@drawable/ic_refresh"

in the ImageButton to something like

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

the recyclerview height becomes normal most items show. The ImageButton is on a layout above RecyclerView. Why does this happen?

nwagu
  • 532
  • 3
  • 17

3 Answers3

0

You have to make your inner linear layout as match parent

<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>
Nupur
  • 31
  • 4
  • Making the LinearLayouts match parent does not work. Though using a mipmap works. But I want to use a drawable in the image button – nwagu May 01 '19 at 13:16
  • Real question is why does it matter to the recyclerview if i use my drawable vs if I use android resource or mipmap – nwagu May 01 '19 at 13:17
  • In the above code with the inner linear layout as match parent, you can use drawable itself. Don't make just linear layout as match parent also I have removed weight from text view. – Nupur May 03 '19 at 06:51
0

Solved it simply by adding

android:fillViewport="true"

to the NestedScrollView

nwagu
  • 532
  • 3
  • 17
-1

Make the layout_height on the Recycler View to match_parent

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Yasiru Nayanajith
  • 1,647
  • 17
  • 20