how to scroll RecyclerView in scrollview

2019-07-31 18:15发布

问题:

how to scroll all above RecyclerView in scrollview

I have to implement RecyclerView in scrollview show as below code, but not scroll RecyclerView.

please give answer

            <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/horizontalScrollView"
            android:layout_marginTop="10dp">

          <RelativeLayout...

                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/rvpouch"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:nestedScrollingEnabled="false"
                            android:layout_below="@+id/textView3">


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

                    </RelativeLayout>

        </ScrollView>

回答1:

Don't use RecyclerView inside ScrollView. Use NestedScrollView instead of ScrollView.

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

For Example:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants">

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

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

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

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

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

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

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

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

Use attribute android:nestedScrollingEnabled="false" for smooth scrolling.



回答2:

Use NestedScrollView instead of scroll view and set

recyclerView.setNestedScrollingEnabled(false);


回答3:

Following code snippet will help you to implement scrolling using ScrollView of RecyclerView

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbarSize="3dp"
    android:scrollbarThumbVertical="@drawable/scrollbar_black">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/rlFiltersSearchEvent"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="@drawable/action_bar_gradient">

        </RelativeLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvListOfEventsMain"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/fab_margin"
            android:layout_below="@+id/rlFiltersSearchEvent"
            android:nestedScrollingEnabled="false"
            android:scrollbars="none" />

    </RelativeLayout>
</ScrollView>

Hope it helps