Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable NestedScrollview scroll

Design of my app

Screen - 1

    <NestedScrollview>
       <LinearLayout orientation:horizontal">
          <RecyclerView-1>
          <Framelayout>(contains Recyclerview-2)
    </NestedScroll>

Screen - 2

     <NestedScrollview>
         <LinearLayout orientation:horizontal">
         <RecyclerView-1>
         <Framelayout> (fragment changed, contains Recyclerview-3)
     </NestedScroll>

Now if user is on screen 1, then both the recyclerview will scroll simultaneously, but on screen 2 if user scrolls RV1 then only RV1 will scroll similarly if RV3 is scrolled then RV3 will be scrolled. Tried all sort of stop scroll, but unable to stop scroll of nested scrollview.

like image 846
Ashlok Avatar asked Jun 09 '18 04:06

Ashlok


People also ask

How do I stop NestedScrollView scrolling?

setnestedscrollingenabled set it to false. try this one add property in recyclerview android:descendantFocusability="blocksDescendants" .

How do I prevent NestedScrollView from scrolling if body is small?

The problem can be solved by moving the SliverAppBar into the CustomScrollView and not use the NestedScrollView at all.

How do I disable scrolling in SwiftUI?

In iOS 16, SwiftUI finally got a new modifier, scrollDisabled(_:) , to disable scrolling in ScrolView and List . Actually, you can use this modifier to disable scrolling for any scrollable views, such as TextEditor .

What is the difference between NestedScrollView and 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.


1 Answers

You must to create a new class that do nothing on touch and scroll events:

public class LockableNestedScrollView extends NestedScrollView {
    // by default is scrollable
    private boolean scrollable = true;

    public LockableNestedScrollView(@NonNull Context context) {
        super(context);
    }

    public LockableNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LockableNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return scrollable && super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return scrollable && super.onInterceptTouchEvent(ev);
    }

    public void setScrollingEnabled(boolean enabled) {
        scrollable = enabled;
    }
}

Next in your layout you change the NestedScroll by your new class:

    <your.package.name.path.LockableNestedScrollView>
       <LinearLayout 
          orientation:"horizontal"
          android:id="@+id/scroll_name">
          <RecyclerView-1>
          <Framelayout>(contains Recyclerview-2)
    </your.package.name.path.LockableNestedScrollView>

Finally in your activity:

LockableNestedScrollView myScrollView = (LockableNestedScrollView) findViewById(R.id.scroll_name);
myScrollView.setScrollingEnabled(false);

I hope it helps someone else.

like image 136
oscarif Avatar answered Oct 06 '22 19:10

oscarif