我有一个RecyclerView(比方说,< code > rootcrecyclerview ),它可以有不同种类的行,这取决于一些API响应。我实现了其中一个是水平的< code > view page 2 ,另一个是用水平的< code > recycle view (比如< code>childRecyclerView)实现的。
rootRecyclerView垂直滑动,而viewPager2和childRecyclerView水平滑动。
问题:
当我在屏幕上滑动时,如果在viewPager2
或childRecyclerView
上滑动,则滑动必须完全水平。否则,它们不会水平滚动;滑动由rootRecyclerView
执行,因此您将看到垂直移动。
因此,发生这种情况是因为您的拇指会在X轴和Y轴上以弯曲/圆形方向移动,从而产生运动,因此rootRecyclerView会拦截滑动,从而产生这种不愉快的用户体验。
我确实尝试过解决这个问题,例如向childRecyclerView添加一个OnItemTouchListener
,如下所示:
private float Y_BUFFER = ViewConfiguration.get(getContext())
.getScaledPagingTouchSlop(); // 10;
private float preX = 0f;
private float preY = 0f;
childRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
if(e.getAction()==MotionEvent.ACTION_DOWN){
childRecyclerView.getParent().requestDisallowInterceptTouchEvent(true);
}
if(e.getAction() == MotionEvent.ACTION_MOVE){
if (Math.abs(e.getX() - preX) > Math.abs(e.getY() - preY)) {
childRecyclerView.getParent().requestDisallowInterceptTouchEvent(true);
} else if (Math.abs(e.getY() - preY) > Y_BUFFER) {
childRecyclerView.getParent().requestDisallowInterceptTouchEvent(false);
}
}
preX = e.getX();
preY = e.getY();
return false;
}
// ... rest of the code
它只解决了孩子RecyclerView
的问题,但我无法为ViewPager2
解决问题。
我也尝试过使用GestureDetector
,如此答案链接中所述,以及一些其他代码组合,但我无法使其工作。
谁能帮我?
好的,所以经过一些研究,我得出的结论是,用一个会“表现得像”视图寻呼机:/的回收站视图
替换我的ViewPager2
。
首先,我将我的view Pager2
替换为水平回收站视图
。要使其表现得像视图寻呼机,请使用SnapHelper
。
RecyclerView childRecyclerView2 = findViewById(R.id.previously_viewPager);
// other init like setup layout manager, adapter etc
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(replacedRecyclerView); // <-- this makes out rv behave like a viewPager
之后,您必须添加一个< code>OnItemTouchListener并覆盖< code > oninterceptouchevent ,就像我问题中的代码段一样:
childRecyclerView2.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
// same as the code segment in the question,
//so skipping this part.
//just copy it from my question
}
// ...
}
在view Pager2中,你可以用get货币项目()
获取当前焦点,但是由于我们已经用回收器视图替换了视图页面2,所以我们没有那个方法。所以,我们需要实现我们自己的等效版本。如果你是一个静态编程语言的人,你可以直接跳转到参考文献2,跳过这部分。这里是java版本,如果你需要,我会跳过解释。创建SnapHelperExt.java
public class SnapHelperExt {
public SnapHelperExt(){}
public int getSnapPosition(RecyclerView recyclerView, SnapHelper snapHelper){
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
View snapView = snapHelper.findSnapView(layoutManager);
if (snapView != null) {
return layoutManager.getPosition(snapView);
}else{
return -1;
}
}
}
接下来创建一个接口OnSnapPositionChangeListener
作为我们的监听器:
public interface OnSnapPositionChangeListener {
void onSnapPositionChange(int position);
}
之后,创建< code > snaponscrolllistener . Java :
public class SnapOnScrollListener extends RecyclerView.OnScrollListener {
public enum Behavior {
NOTIFY_ON_SCROLL,
NOTIFY_ON_SCROLL_STATE_IDLE
}
private SnapHelperExt snapHelperExt;
private SnapHelper snapHelper;
private Behavior behavior;
private OnSnapPositionChangeListener onSnapPositionChangeListener;
private int snapPosition = RecyclerView.NO_POSITION;
public SnapOnScrollListener(SnapHelper snapHelper, Behavior behavior, OnSnapPositionChangeListener onSnapPositionChangeListener){
this.snapHelper = snapHelper;
this.behavior = behavior;
this.onSnapPositionChangeListener = onSnapPositionChangeListener;
this.snapHelperExt = new SnapHelperExt();
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (behavior == Behavior.NOTIFY_ON_SCROLL) {
maybeNotifySnapPositionChange(recyclerView);
}
}
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (behavior == Behavior.NOTIFY_ON_SCROLL_STATE_IDLE
&& newState == RecyclerView.SCROLL_STATE_IDLE) {
maybeNotifySnapPositionChange(recyclerView);
}
}
private void maybeNotifySnapPositionChange(RecyclerView recyclerView){
int prevPosition = this.snapHelperExt.getSnapPosition(recyclerView, snapHelper);
boolean snapPositionIsChanged = (this.snapPosition!=prevPosition);
if(snapPositionIsChanged){
onSnapPositionChangeListener.onSnapPositionChange(prevPosition);
this.snapPosition = prevPosition;
}
}
}
最后,以这种方式使用它:
SnapOnScrollListener snapOnScrollListener = new SnapOnScrollListener(
snapHelper,
SnapOnScrollListener.Behavior.NOTIFY_ON_SCROLL,
position -> {
Log.e(TAG, "currently focused page no = "+position);
// your code here, do whatever you want
}
);
childRecyclerView2.addOnScrollListener(snapOnScrollListener);
参考文献: