Disable scrolling in child program Recyclerview android

I have a layout consisting of a Parent RecyclerView with an additional Recyclerview in it

I know that it’s not good to put a list inside another list, but I need it so that I can use the auxiliary list functions like scrolling and dragging

My problem is that the child Recyclerview gets focus and stops the parent from scrolling if there was a touch point on it, I just want the touch to be vertical to the child Recyclerview parent scroll up and down, and if the touch was horizontal or click, then the list item Recyclerview of a child moves left and right. Any help to achieve this?

+14
android android-listview android-recyclerview
May 13 '15 at 18:10
source share
9 answers

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 create it so that the vertical scroll

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

Finally, set the custom layout as the recycler view layout manager

 recyclerView.setLayoutManager(customLayoutManager); 
+39
Aug 27 '15 at 13:49
source share

While it may not be a good practice to have inbuilt recycler scans, sometimes you cannot avoid this. Maybe something like this:

 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 event. Use this class for a child RecyclerView. You want the PARENT repeater to scroll, but not the baby. Well, this should do it, since the parent will be just a standard RecyclerView, but the child will be so normal, without scrolling, but will handle clicks. You may need to disable the click for the parent RecyclerView. Not sure, since I have not tested this, so consider it as an example ...

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

 <com.yourpackage.location.NoScrollRecycler ... ... > ... ... </com.yourpackage.location.NoScrollRecycler> 
+15
May 13 '15 at 18:34
source share

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

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

In some way, if you use the coordinator layout, in case you want to simplify the situation, 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 should specify the application: layout_behavior

 app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
+13
Mar 28 '16 at 12:21
source share

android:nestedScrollingEnabled="false" in a child RecyclerView

You can add

 android:nestedScrollingEnabled="false" 

for your RecyclerView in XML or

 childRecyclerView.setNestedScrollingEnabled(false); 

for your RecyclerView in Java.

EDIT: -

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

 ViewCompat.setNestedScrollingEnabled(childRecyclerView, false); 
+10
Dec 05 '16 at 10:31
source share

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

In my case, the code was

mInnerRecyclerView.setNestedScrollingEnabled(false); where mInnerRecyclerView is an internal RecyclerView .

+2
Mar 15 '16 at 19:28
source share

I tried many suggested solutions and could not find the one that worked in my case. I have more than 1 RecyclerView inside ScrollView using GridLayoutManager. The result from the above suggestion caused ScrollView to stop scrolling whenever I lifted my finger (it did not slide on the top or bottom of the image when my finger was lifted above the RecyclerView)

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

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

If you override them in a custom layout manager, return false and it will stop scrolling. It also fixes an issue when ScrollView stops scrolling.

+1
Aug 21 '15 at 8:43
source share

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

EDIT: But unfortunately, it also blocks events for the list item.

0
Aug 24 '15 at 7:41
source share

I think I was late, but here I found a solution if it still annoys 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; } }); 
0
Oct 23 '18 at 15:59
source share
  <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 in a linner layout ... no need to do anything pragmatically

0
May 26 '19 at 2:42
source share



All Articles