LeTadas
LeTadas

Reputation: 3556

RecyclerView height wrap_content doesn't work in NestedScrollView

So I have recyclerView with height wrap_content as I understand this means that recyclerView should expand and collapse whenever I add items to it or remove, but for some reason in NestedScrollView it doesn't behave like that when I add new Objects to it, it stays same size as it was any ideas what could be wrong? Tried adding

android:fillViewport="true"

But no reactions as far as I understand it could be because RecyclerView not getting updated when children of it changes, because when I restart app it collapses or expands to proper size.

Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/icons"
    android:orientation="vertical">


<LinearLayout
    android:id="@+id/main_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="vertical">

    <include layout="@layout/toolbar" />


        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested"
            android:fillViewport="true"
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <RelativeLayout
                    android:id="@+id/empty_group_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:visibility="gone">

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true">

                        <TextView
                            android:id="@+id/empty_text_first"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:text="@string/no_group_first"
                            android:textColor="@color/secondary_text"
                            android:textSize="17sp" />

                        <ImageView
                            android:id="@+id/add_icon"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_toEndOf="@+id/empty_text_first"
                            android:src="@drawable/ic_add_group_gry" />

                        <TextView
                            android:id="@+id/empty_text_second"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_toEndOf="@+id/add_icon"
                            android:text="@string/no_group_second"
                            android:textColor="@color/secondary_text"
                            android:textSize="17sp" />

                    </RelativeLayout>
                </RelativeLayout>

                <TextView
                    android:textSize="14sp"
                    android:layout_marginStart="6dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Favorite groups"/>


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/favorite_group_list"
                    android:nestedScrollingEnabled="true"
                    android:layout_width="match_parent"
                    android:layout_height="156dp"
                    android:layout_gravity="center"
                    android:background="@color/icons"
                    android:padding="6dp">
                </android.support.v7.widget.RecyclerView>

                <TextView
                    android:textSize="14sp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginStart="6dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Groups"/>

                <android.support.v7.widget.RecyclerView
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    android:id="@+id/group_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/background"
                    android:nestedScrollingEnabled="false"
                    android:padding="6dp">

                </android.support.v7.widget.RecyclerView>

            </LinearLayout>

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

</LinearLayout>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/add_new_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:src="@drawable/ic_playlist_add_48px"
    app:behavior_autoHide="true"
    app:layout_anchor="@id/main_holder"
    app:layout_anchorGravity="bottom|right|end" />

Upvotes: 3

Views: 1452

Answers (2)

pauminku
pauminku

Reputation: 3596

Probably the problem is related with the adapter's layout. Be sure to put some fixed value or wrap_content on the adapter's layout height. If it's match_parent it won't work as expected because each row will fill all the RecyclerView

Upvotes: 0

Shanmugam
Shanmugam

Reputation: 301

Hi kosas give wrap _content for recycler view and while setting layout manager follow this

 linearLayoutManager = new LinearLayoutManager(getActivity()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

Upvotes: 0

Related Questions