禁用NestedScrollview滚动

问题描述 投票:3回答:1

我的应用程序的设计

屏幕 - 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。尝试了所有类型的停止滚动,但无法停止嵌套滚动视图的滚动。

android android-recyclerview android-framelayout android-nestedscrollview
1个回答
3
投票

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

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

我希望它可以帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.