这个问题对我来说似乎很有趣。所以我尝试实现,这就是我实现的(你也可以看到 video here ),非常顺利。
所以你可以试试这样的:
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 最终会处理滚动。
Tôi là một lập trình viên xuất sắc, rất giỏi!