sách gpt4 ăn đã đi

android - 嵌套的 RecyclerView。如何防止父 RecyclerView 在子 RecyclerView 滚动时滚动?

In lại Tác giả: IT Lão Cao 更新时间:2023-10-28 21:57:39 30 4
mua khóa gpt4 giày nike

我正在尝试实现一个水平的 recyclerview 并且 recyclerview 的每个项目都将是一个具有网格布局的垂直 recyclerview。我面临的问题是,当我尝试垂直滚动子 recyclerview 时,有时父 recyclerview 会滚动并开始水平滚动。我试图解决这个问题的方法是,

  1. setNestedScrollingEnabled(false) 在父 recyclerview
  2. 在子 recyclerview của onTouch() 中,我通过调用 requestdisallowinterceptTouchevent(false) 禁用父 recyclerview 上的触摸事件

以上解决方案都不能完美解决问题。任何帮助表示赞赏

câu trả lời hay nhất

这个问题对我来说似乎很有趣。所以我尝试实现,这就是我实现的(你也可以看到 video here ),非常顺利。

nhập mô tả hình ảnh ở đây

所以你可以试试这样的:

sự định nghĩa CustomLinearLayoutManager Mở rộng LinearLayoutManager Như thế này:

public class CustomLinearLayoutManager extends LinearLayoutManager {

public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}

@Ghi đè
public boolean canScrollVertically() {
trả về false;
}
}

并将此 CustomLinearLayoutManager 设置为您的父 RecyclerView

RecyclerView parentRecyclerView = (RecyclerView)findViewById(R.id.parent_rv);
CustomLinearLayoutManager customLayoutManager = new CustomLinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false);
parentRecyclerView.setLayoutManager(customLayoutManager);
parentRecyclerView.setAdapter(new ParentAdapter(this)); // some adapter

现在对于子 RecyclerView,定义自定义 CustomGridLayoutManager Mở rộng GridLayoutManager:

public class CustomGridLayoutManager extends GridLayoutManager {

public CustomGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

public CustomGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}

public CustomGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}

@Ghi đè
public boolean canScrollHorizontally() {
trả về false;
}
}

并将其设置为 layoutManger 到子 RecyclerView:

childRecyclerView = (RecyclerView)itemView.findViewById(R.id.child_rv);
childRecyclerView.setLayoutManager(new CustomGridLayoutManager(context, 3));
childRecyclerView.setAdapter(new ChildAdapter()); // some adapter

所以基本上父 RecyclerView 只听水平滚动,子 RecyclerView 只听垂直滚动。

除此之外,如果您还想处理对角滑动(几乎不偏向垂直或水平),您可以在 parent RecylerView 中包含一个手势监听器>.

public class ParentRecyclerView extends RecyclerView {

private GestureDetector mGestureDetector;

public ParentRecyclerView(Context context) {
siêu(bối cảnh);
mGestureDetector = new GestureDetector(this.getContext(), new XScrollDetector());
// do the same in other constructors
}

// and override onInterceptTouchEvent

@Ghi đè
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
}

}

XScrollDetector 在哪里

class XScrollDetector extends GestureDetector.SimpleOnGestureListener {
@Ghi đè
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return Math.abs(distanceY) < Math.abs(distanceX);
}
}

Vì vậy ParentRecyclerView 要求 subview (在我们的例子中为 VerticalRecyclerView)来处理滚动事件。如果 subview 处理,那么父 View 不会做任何事情,父 View 最终会处理滚动。

关于android - 嵌套的 RecyclerView。如何防止父 RecyclerView 在子 RecyclerView 滚动时滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36286391/

30 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress