How to use RecyclerView inside NestedScrollView?

前端 未结 24 2405
抹茶落季
抹茶落季 2020-11-22 03:39

How to use RecyclerView inside NestedScrollView? RecyclerView content is not visible after setting adapter.

UPDATE

相关标签:
24条回答
  • 2020-11-22 04:15

    RecyclerView with NestedScrollView

        <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    0 讨论(0)
  • 2020-11-22 04:17

    You can use android:fillViewport="true" to make NestedScrollView measure the RecyclerView. The RecyclerView will fill the remaining height. so if you want to scroll the NestScrollView, you can set the RecyclerView's minHeight.

    0 讨论(0)
  • 2020-11-22 04:17

    If you are using RecyclerView-23.2.1 or later. Following solution will work just fine:

    In your layout add RecyclerView like this:

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

    And in your java file:

    RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    LinearLayoutManager layoutManager=new LinearLayoutManager(getContext());
    layoutManager.setAutoMeasureEnabled(true);
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setAdapter(new YourListAdapter(getContext()));
    

    Here layoutManager.setAutoMeasureEnabled(true); will do the trick.

    Check out this issue and this developer blog for more information.

    0 讨论(0)
  • 2020-11-22 04:17

    I had to implement CoordinatorLayout with toolbar scrolling and it just took me all the day messing around this. I've got it working by removing NestedScrollView at all. So I'm just using RelativeLayout at the root.

    <?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:layout_width="match_parent"
        android:layout_height="match_parent">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_nearby"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-22 04:20

    Simply adding recyclerView.setNestedScrollingEnabled(false); before setAdapter itself worked for me. I didn't add app:layout_behavior="@string/appbar_scrolling_view_behavior" anywhere & didn't set any custom layout manager

    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:orientation="vertical">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/white"
                    android:text="Some Text..."
                    android:padding="15dp" />
    
            </LinearLayout>
    
            <LinearLayout
                android:orientation="vertical"
                android:padding="15dp"
                android:layout_marginTop="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Quick Links"
                    android:textColor="@color/black"
                    android:textStyle="bold"
                    android:textAllCaps="true"
                    android:paddingLeft="20dp"
                    android:drawableLeft="@drawable/ic_trending_up_black_24dp"
                    android:drawablePadding="10dp"
                    android:layout_marginBottom="10dp"
                    android:textSize="16sp"/>
    
                <View
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:background="#efefef"/>
    
                <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/recyclerview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
    
        </LinearLayout>
    
    </android.support.v4.widget.NestedScrollView>
    
    0 讨论(0)
  • 2020-11-22 04:21

    1) You need to use support library 23.2.0 (or) above

    2) and RecyclerView height will be wrap_content.

    3) recyclerView.setNestedScrollingEnabled(false)

    But by doing this the recycler pattern doesn't work. (i.e all the views will be loaded at once because wrap_content needs the height of complete RecyclerView so it will draw all child Views at once. No view will be recycled). Try not to use this pattern unless it is really required. Try to useviewTypeand add all other views that need to scroll toRecyclerViewrather than usingRecyclerViewinScrollview`. The performance impact will be very high.

    To make it simple "it just acts as LinearLayout with all the child views"

    0 讨论(0)
提交回复
热议问题