当子recyclerview滚动到最后一项时,禁用ViewPager分页 [英] Disable ViewPager paging when child recyclerview scrolls to last item

查看:182
本文介绍了当子recyclerview滚动到最后一项时,禁用ViewPager分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个viewpager,在其中一个片段中,我有两个分别包含垂直和水平recyclerview的片段.

I have a viewpager and within one of the fragments I have two separate fragments containing a vertical and a horizontal recyclerview respectively.

当我将水平回收站视图滚动到最后一项并尝试进一步滑动时,viewpager滚动到下一页.我不希望这种情况发生.当我尝试过度滚动水平recyclerview时,我想禁用viewpager的页面调度.

When I scroll the horizontal recyclerview to the last item and try to swipe further, the viewpager scrolls to the next page. I do not want this to happen. I want to disable the viewpager's paging when I try to overscroll the horizontal recyclerview.

但是,我不想在其他任何地方滑动时都禁用viewpager的页面调度.例如,如果我要在垂直的recyclerview或父片段中的任何空白处滑动,它仍应导致viewpager更改页面.

However, I do not want to disable the viewpager's paging when I swipe anywhere else. For example, If I were to swipe on the vertical recyclerview or any empty space within the parent fragment, it should still cause the viewpager to change pages.

我阅读了这个问题的相似之处在于有一个子viewpager,但是我尝试使用水平recyclerview复制该视图并没有成功.

I read in this SO question, how to disable paging of the viewpager. Also this SO question is similar in that there is a child viewpager, however I have not had success trying to replicate that with a horizontal recyclerview.

这里是一些结构:

允许我禁用分页的自定义viewpager(从上面的第一个SO链接中获取):

The custom viewpager which allows me to disable paging(took it from first SO link above):

public class CustomViewPager extends ViewPager {

private boolean enabled;

public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.enabled = true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onTouchEvent(event);
    }

    return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onInterceptTouchEvent(event);
    }

    return false;
}

public void setPagingEnabled(boolean enabled) {
    this.enabled = enabled;
} }

对于卧式回收站视图,我设置了一个ontouchlistener(类似于上面的第二个SO链接):

For the horizontal recyclerview I set an ontouchlistener(similar to second SO link above):

horizontalRecyclerView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch(event.getAction()){
                        case MotionEvent.ACTION_MOVE:
                            customViewPager.setPagingEnabled(false);
                            break;
                        case MotionEvent.ACTION_UP:
                            customViewPager.setPagingEnabled(true);
                            break;
                    }
                    return false;
                }
            });

其他观察结果:我注意到,有时如果我在滑动之前长时间按下卧式回收机视图,它将不会转到viewpager的下一页.但是,如果我快速滑动,viewpager将转到下一页.

Extra observations: I noticed that sometimes if I long press the horizontal recyclerview before I swipe it will not go to the next page of the viewpager. However, if I swipe quickly the viewpager will go to the next page.

有人知道这样做的正确方法吗?

Does anyone know the proper way to do this?

感谢您的帮助.

推荐答案

这是解决方法.

requestDisallowInterceptTouchEvent()

此方法不允许父母移动.它将停止viewpager触摸事件.

This method will not allow parent to move. It stops viewpager touch event.

horizontalRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            int action = e.getAction();
            switch (action) {
                case MotionEvent.ACTION_MOVE:
                    rv.getParent().requestDisallowInterceptTouchEvent(true);
                    break;
            }
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    });

这篇关于当子recyclerview滚动到最后一项时,禁用ViewPager分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆