[英]How to achieve vertical scrolling on a horizontal recyclerview and blocking moving to next page of viewpager when scrolled on horizontal recycler view
There is a ViewPager
, Horizontal RecyclerView
and a ScrollView
. 有一个
ViewPager
,Horizontal RecyclerView
和一个ScrollView
。 When the last item on the RecyclerView
is reached, on swiping horizontally, the next page in the ViewPager
is shown. 到达
RecyclerView
上的最后一项时,在水平滑动时,将显示ViewPager
中的ViewPager
。 As this is not that user friendly, I disabled moving to next page when swiped horizontally on the RecyclerView
. 由于这不是用户友好的,我在
RecyclerView
上水平滑动时禁用了移动到下一页。 But the RecyclerView
is a part of the ScrollView
. 但
RecyclerView
是ScrollView
的一部分。 So the problem is, when I swipe up and down on the RecyclerView
, the page does not go up or down. 所以问题是,当我在
RecyclerView
上上下滑动时,页面不会上升或下降。 The page goes up and down only when swiped outside the RecyclerView
. 只有在
RecyclerView
外部滑动时,页面才会上下移动。 Is there a possibility to achieve both? 是否有可能同时实现这两个目标? ie, move up and down the page when swiped on the
RecyclerView
and do not move to the next page when swiped horizontally on the RecyclerView
. 即,在
RecyclerView
上刷卡时上下移动页面,在RecyclerView
上水平滑动时不要移动到下一页。 All this is implemented as a fragment and not as an activity. 所有这些都是作为片段而不是作为活动来实现的。
The below code is used to disable moving to next page when swiped horizontally on the RecyclerView
: 以下代码用于在
RecyclerView
上水平滑动时禁用移动到下一页:
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent event){
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_MOVE:
rv.getParent().requestDisallowInterceptTouchEvent(true);
break;
}
return false;
}
Use custom Viewpager to block swip 使用自定义Viewpager阻止swip
public class NonSwipeableViewPager extends ViewPager {
public NonSwipeableViewPager(Context context) {
super(context);
setMyScroller();
}
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
setMyScroller();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
//down one is added for smooth scrolling
private void setMyScroller() {
try {
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
} catch (Exception e) {
e.printStackTrace();
}
}
public class MyScroller extends Scroller {
public MyScroller(Context context) {
super(context, new DecelerateInterpolator());
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.