简体   繁体   English

如何在水平回收视图上实现垂直滚动,并在水平回收视图上滚动时阻止移动到viewpager的下一页

[英]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 ,Horizo​​ntal 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 . RecyclerViewScrollView的一部分。 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.

相关问题 ViewPager内部的水平Recycler视图不滚动 - Horizontal Recycler View inside ViewPager not scrolling 如何实现水平列表但垂直滚动的recyclerview android - How to achieve recyclerview with horizontal list but vertical scrolling android android回收站视图中的水平和垂直滚动 - Horizontal and vertical scrolling in android recycler view 如何修复垂直 RecyclerView 内的水平 ViewPager2 和 RecyclerView 的滚动问题? - How to fix Scrolling issue of horizontal ViewPager2 and RecyclerView that are inside a vertical RecyclerView? ScrollView中的Horizo​​ntal RecyclerView:垂直滚动时如何获得焦点? - Horizontal RecyclerView inside ScrollView: how to get focus when vertical scrolling? ViewPager 内的水平 RecyclerView 不滚动 - Horizontal RecyclerView inside ViewPager is not scrolling 如何在RecyclerView中实现水平和垂直滚动? - How to implement Horizontal and Vertical Scrolling in RecyclerView? Viewpager具有水平和垂直滚动 - Viewpager with horizontal as well as vertical scrolling 如何使用水平滚动创建Recycler网格视图 - How to create a Recycler Grid View with horizontal scrolling 在水平和垂直的recyclerview中为布局充气,带有回收站视图的FlexboxLayout将视图水平自动垂直添加 - inflating a layout in recyclerview in horizontal and vertical, FlexboxLayout with recycler view add view horizontal automatically vertical
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM