禁用NestedScrollview滚动

时间:2018-06-09 04:44:21

标签: android android-recyclerview android-framelayout android-nestedscrollview

我的应用程序的设计

屏幕 - 1

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

屏幕 - 2

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

现在,如果用户在屏幕1上,那么recyclelerview将同时滚动,但是如果用户滚动RV1,则在屏幕2上,如果RV3滚动,则RV1将同样滚动,然后滚动RV3。尝试了所有类型的停止滚动,但无法停止嵌套滚动视图的滚动。

1 个答案:

答案 0 :(得分:1)

您必须创建一个在触摸和滚动事件上不执行任何操作的新类:

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;
    }
}

接下来,在布局中,通过新类更改NestedScroll:

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

最后在您的活动中:

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

我希望它可以帮助其他人。