在子Recyclerview android中禁用滚动

Abd*_*ssa 16 android android-listview android-recyclerview

我的布局包含一个带有子Recyclerview的Parent RecyclerView

我知道将列表放在另一个列表中是不好的但我必须这样我才能使用子列表功能,如滑动和拖放

我的问题是,孩子Recyclerview获得焦点并阻止父母滚动,如果触摸点在它上面只是我想如果触摸是垂直在孩子上Recyclerview父母上下滚动,如果触摸是水平或点击然后子Recyclerview列表项左右滑动.有没有帮助实现这一目标?

Beh*_*yar 41

我终于找到了解决方案.

创建自定义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;
}
}
Run Code Online (Sandbox Code Playgroud)

然后像这样实例化它以进行垂直滚动

CustomLinearLayoutManager customLayoutManager = new CustomLinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
Run Code Online (Sandbox Code Playgroud)

最后将自定义布局设置为回收器视图的布局管理器

recyclerView.setLayoutManager(customLayoutManager);
Run Code Online (Sandbox Code Playgroud)


Luc*_*ord 15

虽然嵌入回收站视图可能不是一个好习惯,但有时您无法避免.像这样的东西可能会起作用:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这将禁用滚动事件,但不会禁用单击事件.将此类用于"child"RecyclerView.您希望PARENT recyclerview能够滚动,而不是孩子.那么这应该这样做,因为父母将只是标准的RecyclerView,但孩子将是这个自定义的没有滚动,但处理点击.可能需要禁用单击父级RecyclerView ..不确定,因为我没有测试过这个,所以请考虑它只是一个例子......

另外,要在XML中使用它(如果您不知道),请执行以下操作:

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

     ...
     ...

</com.yourpackage.location.NoScrollRecycler>
Run Code Online (Sandbox Code Playgroud)


小智 15

在您的ActivityName.java上,在onCreate()方法内写入:

RecyclerView v = (RecyclerView) findViewById(R.id.your_recycler_view_id);
v.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)

无论如何,如果您正在使用协调器布局,如果您想简化操作,并且您想要禁用嵌套滚动.

 <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>
Run Code Online (Sandbox Code Playgroud)

再次,您应用相同的原则:在您的ActivityName.java上,在onCreate()方法内写:

RecyclerView v = (RecyclerView) findViewById(R.id.your_recycler_view_id);
v.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)

所以基本上在XML中,你必须指定app:layout_behavior

app:layout_behavior="@string/appbar_scrolling_view_behavior">
Run Code Online (Sandbox Code Playgroud)

  • 解决了我的问题,即使将RecycleView设置为wrap_content,支持RecycleView的控件仍在ScrollView中滚动。用支持NestedScrollView替换了ScrollView,所有工作都像预期的那样。谢谢! (2认同)

jaf*_*ech 12

android:nestedScrollingEnabled="false" 在孩子RecyclerView

你可以加

android:nestedScrollingEnabled="false"
Run Code Online (Sandbox Code Playgroud)

用XML或XML格式的RecyclerView

childRecyclerView.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)

到您的RecyclerView in Java.

编辑:-

childRecyclerView.setNestedScrollingEnabled(false);将仅适用于android_version> 21台设备.要在所有设备上工作,请使用以下命令

ViewCompat.setNestedScrollingEnabled(childRecyclerView, false);
Run Code Online (Sandbox Code Playgroud)