Viewpager拦截小吃店辞退 [英] Viewpager intercepts snackbar dismiss

查看:338
本文介绍了Viewpager拦截小吃店辞退的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

范围

我有一个包含ViewPager片段。在viewpager的每个页面都包含某个项目的片段。当我把该项目片段的行动,我显示该项目的片段中的一个小吃店。的小吃店是物品片段的部分(CoordinatorLayout是物品的碎片的布局的一部分)。

I have a fragment that contains a ViewPager. Each page of the viewpager contains an item fragment. When I take an action on the item fragment, I display a snackbar within the item fragment. The snackbar is part of the item fragment (the CoordinatorLayout is part of the item fragments layout).

问题

我现在面临的问题是,我不允许的,因为viewpager拦截刷卡事件并更改页面,而不是让小吃店得到驳回驳回小吃店。

The problem I am facing is that I am not allowed to dismiss the snackbar because the viewpager intercepts the swipe event and changes pages instead of letting the snackbar get dismissed.

我想viewpager不拦截在小吃店触动,但还是拦截的项目片段的剩余触摸(用户仍然应该能够滑动即可切换时不刷卡的小吃店页)。有谁知道的一种方式做到这一点?

I would like the viewpager not to intercept touches on the snackbar, but still intercept touches on the remainder of the item fragment (the user should still be able to swipe to change pages when not swiping on the snackbar). Does anyone know of a way to do this?

我创建了一个示例应用来说明问题。它可在 https://github.com/gfrederick/ViewPagerSnackbar

I have created a sample app to demonstrate the problem. It is available at https://github.com/gfrederick/ViewPagerSnackbar

推荐答案

看看我的解决方案 github上

Check out my solution on github.

在情况下,链接取下来出于某种原因,我将解释我做了什么。

In case the link is taken down for some reason I'll explain what I did.


  1. 我复制相关的小吃吧班到我的项目。

  2. 这个答案以一个类似的问题,我修改小吃吧的行为,子类在viewpager启发。具体来说,我觉得如果有一个viewpager作为视图层次的小吃店的父母。然后,当小吃吧触摸我禁用viewpagers处理触摸事件。我重新启用它让小吃吧(当触摸事件已经结束)的去当。

  1. I copied the relevant Snackbar classes into my project.
  2. Inspired by this answer to a similar question I modified the Behavior subclass of Snackbar to work in a viewpager. Specifically, I find if there is a viewpager as a parent of the snackbar in the view hierarchy. Then when the Snackbar is touched I disable that viewpagers handling of touch events. I reenable it when let go of Snackbar (when the touch event is over).

下面是最重要code:

Here's the important code:

final class Behavior extends SwipeDismissBehavior<SnackbarLayout> {

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, SnackbarLayout child,
                                         MotionEvent event) {

        ViewPager vp = getViewPagerParent(child);

        if (parent.isPointInChildBounds(child, (int) event.getX(), (int) event.getY())) {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    ViewPagerSnackbarManager.getInstance().cancelTimeout(mManagerCallback);

                    // If touching Snackbar tell the viewpager not to intercept touch events
                    if (vp != null) {
                        vp.requestDisallowInterceptTouchEvent(true);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    // As soon as this event (touching the Snackbar) is over tell viewpager to resume intercepting touch events
                    if (vp != null) {
                        vp.requestDisallowInterceptTouchEvent(false);
                    }
                    ViewPagerSnackbarManager.getInstance().restoreTimeout(mManagerCallback);
                    break;
            }
        }
        return super.onInterceptTouchEvent(parent, child, event);
    }

    // helper method that move up the view hierarchy searching for a Viewpager and returns it if found. Null if not found.
    private ViewPager getViewPagerParent(View child) {
        ViewParent parent = child.getParent();

        while (parent != null) {
            parent = child.getParent();
            if (parent instanceof ViewPager) {
                return (ViewPager) parent;
            } else if (!(parent instanceof View)) {
                return null;
            } else {
                child = (View) parent;
            }
        }
        return null;
    }
}

这篇关于Viewpager拦截小吃店辞退的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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