Disable Scrolling in child Recyclerview android

做~自己de王妃 提交于 2019-12-17 10:42:32

问题


I have a layout consists of a Parent RecyclerView with a sub Recyclerview in it

i know that it is not good to put a list inside another list but i have to so that i can use the sub list features like swiping and drag and drop

My issue is that the child Recyclerview gain focus and stops the parent from scrolling if the touch point was on it simply i want if the touch was vertically on the child Recyclerview the parent scrolls up and down and if the touch was horizontal or a click then the child Recyclerview list item swipes left and right. Any help to achieve this?


回答1:


I finally found a solution.

Create Custom LinearLayoutManager

public class CustomLinearLayoutManager extends LinearLayoutManager {
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);

}

// it will always pass false to RecyclerView when calling "canScrollVertically()" method.
@Override
public boolean canScrollVertically() {
    return false;
}
}

Then instantiate it like this for vertical scrolling

CustomLinearLayoutManager customLayoutManager = new CustomLinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);

Finally set the custom layout as layout manager of recycler view

recyclerView.setLayoutManager(customLayoutManager);



回答2:


While it might not be good practice to have embedded recycler views, sometimes you cannot avoid it. Something like this might work:

public class NoScrollRecycler extends RecyclerView {

    public NoScrollRecycler(Context context){
        super(context);
    }

    public NoScrollRecycler(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    public NoScrollRecycler(Context context, AttributeSet attrs, int style){
        super(context, attrs, style);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){

        //Ignore scroll events.
        if(ev.getAction() == MotionEvent.ACTION_MOVE)
            return true;

        //Dispatch event for non-scroll actions, namely clicks!
        return super.dispatchTouchEvent(ev);
    }
}

This will disable the scroll event, but not the click events. Use this class for the "child" RecyclerView. You want the PARENT recyclerview to scroll, but not the child. Well this should do that, since the parent will just be the standard RecyclerView, but the child will be this custom one with no scrolling, but handles clicks. Might need to disable clicking for the parent RecyclerView.. Not sure as I have not tested this, so consider it just an example...

Also, to use this in XML (incase you didn't know) do the following:

<com.yourpackage.location.NoScrollRecycler
     ...
     ... >

     ...
     ...

</com.yourpackage.location.NoScrollRecycler>



回答3:


On your ActivityName.java, inside the onCreate() method write:

RecyclerView v = (RecyclerView) findViewById(R.id.your_recycler_view_id);
v.setNestedScrollingEnabled(false);

By any means, if you are using Coordinator Layout, In case you want to simplify things, and you want to disable nested scrolling.

 <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/activitiesListRV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
 </android.support.v4.widget.NestedScrollView>

And again you apply the same principle: On your ActivityName.java, inside the onCreate() method write:

RecyclerView v = (RecyclerView) findViewById(R.id.your_recycler_view_id);
v.setNestedScrollingEnabled(false);

So basically in XML, you have to specify the app: layout_behavior

app:layout_behavior="@string/appbar_scrolling_view_behavior">



回答4:


android:nestedScrollingEnabled="false" in the child RecyclerView

You can add

android:nestedScrollingEnabled="false"

to your RecyclerView in XML or

childRecyclerView.setNestedScrollingEnabled(false);

to your RecyclerView in Java.

EDIT:-

childRecyclerView.setNestedScrollingEnabled(false); will work only in android_version>21 devices. to work in all devices use the following

ViewCompat.setNestedScrollingEnabled(childRecyclerView, false);



回答5:


you can use setNestedScrollingEnabled(false); on sub RecyclerView which stops scrolling inside sub RecyclerView.

In my case code was

mInnerRecyclerView.setNestedScrollingEnabled(false); where mInnerRecyclerView being inner RecyclerView.




回答6:


I've tried many suggested solutions and couldn't find one that worked in my case. I have more than 1 RecyclerView inside a ScrollView using a GridLayoutManager. The result from the suggestion above resulted in the ScrollView stopping to scroll whenever I lifted my finger (it didn't glide to the top or bottom of the view when my finger was lifted over a RecyclerView)

Looking through the RecyclerView source, inside the onTouchEvent there is a call to the layout manager:

final boolean canScrollHorizontally = mLayout.canScrollHorizontally();
final boolean canScrollVertically = mLayout.canScrollVertically();

If you override these in a custom layout manager, return false and it will stop scrolling. It also fixes the problem where the ScrollView would stop scrolling abruptly.




回答7:


Ithink I'm too late but here i found the solution if it's still annoying someone:

    RecyclerView v = (RecyclerView); 
    findViewById(R.id.your_recycler_view_id);
    v.setNestedScrollingEnabled(false);
    sensorsRecyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent    event) {
            return true;
        }
    });



回答8:


 <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv"
                android:layout_marginTop="2dp"
                android:layout_marginLeft="2dp"
                android:layout_marginBottom="10dp"
                android:layout_marginRight="2dp"
                android:nestedScrollingEnabled="false"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />
                </RelativeLayout>

put the code inside linner layout ...no need to do anything pragmatically




回答9:


If you don't want make a custom view, another option is to create a same sized layout in front of the RecyclerView, and make it clickable.

EDIT: But unfortunately it blocks events for list item too.



来源:https://stackoverflow.com/questions/30222310/disable-scrolling-in-child-recyclerview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!