简体   繁体   中英

How to disable child(RecyclerView) scrolling before parent(ScrollView) has completely scrolled

I have a RecyclerView inside a ScrollView. When I try to scroll to bottom, the RecyclerView(child element) scrolls to bottom before the parent starts scrolling. But what I want it, the parent should scroll completely before child starts scrolling. Here is my layout file. Can someone please guide me how can I achieve this?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:id="@+id/blank_layout"
            android:orientation="vertical">
        </LinearLayout>

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:id="@+id/blank_layout"
            android:orientation="vertical">
        </LinearLayout>

    </LinearLayout>

</ScrollView>

</RelativeLayout>

Disable RecyclerView. Extend scrollview so that you will know when ot has reached bottom as bellow.

public class myScrollView extends ScrollView
{
    public myScrollView(Context context)
    {
        super(context);
    }
    public myScrollView(Context context, AttributeSet attributeSet)
    {
        super(context,attributeSet);
    }

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
    View view = (View)getChildAt(getChildCount()-1);
    int d = view.getBottom();
    d -= (getHeight()+getScrollY());
    if(d==0)
    {
        //you are at the end of the list in scrollview 
        //do what you wanna do here
    }
    else
        super.onScrollChanged(l,t,oldl,oldt);
}

}

Once your scrollViewReaches bottom enable recyclerView. Give it a go. If you are not able to achieve what you desire using this method let me know.

Just use NestedScrollView - it supports having nested scrolling enabled views (such as RecyclerView ) inside of it. Make sure you are using Support Library 23.2 , which allows RecyclerView to measure itself based on its contents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM