简体   繁体   English

将触摸事件分配给ViewPager的未聚焦片段

[英]Dispatch touch events to ViewPager's unfocused fragments

I've implemented Dave Smith's elegant solution to displaying multiple views inside a ViewPager here , but am having trouble dispatching touch events to the fragments that are not the "focused" one. 我已经实现了Dave Smith的优雅解决方案, 可以在这里在ViewPager中显示多个视图 ,但是在将触摸事件分配给不是“集中”的片段时遇到了麻烦。

In his PagerContainer solution, there is functionality to handle the touch events outside of the ViewPager's focused area (see below), but that's only to enable scrolling. 在他的PagerContainer解决方案中,有一些功能可以处理ViewPager的关注区域之外的触摸事件(请参见下文),但这仅仅是为了实现滚动。 I need those touch events to actually interact with the views on the fragments themselves. 我需要这些触摸事件才能真正与片段本身上的视图进行交互。

Does anyone have any experience with this? 有人对这个有经验么?

@Override
public boolean onTouchEvent(MotionEvent ev) {
    //We capture any touches not already handled by the ViewPager
    // to implement scrolling from a touch outside the pager bounds.
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mInitialTouch.x = (int)ev.getX();
            mInitialTouch.y = (int)ev.getY();
        default:
            ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y);
            break;
    }

    return mPager.dispatchTouchEvent(ev);
}

How do I get the touch events from the PagerContainer propagated to the appropriate fragment? 如何从PagerContainer中获取触摸事件,并传播到适当的片段?

You can use Event bus for this purpose checkout this link how to use it.I have used it for this kind of communications between activities and fragments. 您可以将事件总线用于此目的,以查看此链接的用法。我已将其用于活动和片段之间的这种通信。 https://github.com/greenrobot/EventBus https://github.com/greenrobot/EventBus

So, I've got a partial solution implemented but it's not the final answer. 因此,我实现了部分解决方案,但这不是最终的答案。 In the OnTouchEvent overridden in the PagerContainer, I'm now checking the HitRect of each fragment and determining what fragment the event's point is inside. 在PagerContainer中重写的OnTouchEvent中,我现在要检查每个片段的HitRect并确定事件的点位于哪个片段中。 The problem now? 现在有问题吗? How to stop scrolling to the fragment that's been selected. 如何停止滚动到选定的片段。

Here's what I've tried, but it's still scrolling to the clicked on fragment. 这是我尝试过的方法,但仍在滚动到单击的片段。 Any ideas on how to stop this? 关于如何停止这种想法?

@Override
public boolean onTouchEvent(MotionEvent evt) {

    //We capture any touches not already handled by the ViewPager
    // to implement scrolling from a touch outside the pager bounds.
    int fragCount = mPager.getAdapter().getCount();
    for(int i = 0; i < fragCount; i++)
    {
        View view = mPager.getChildAt(i);
        Rect rect = new Rect();
        view.getHitRect(rect);

        if(rect.contains((int)evt.getX(),(int)evt.getY()))
        {
            int currentItem = mPager.getCurrentItem();
            if(currentItem != i)
            {
                mPager.clearOnPageChangeListeners();
                mPager.setCurrentItem(currentItem + i, false);
                mPager.addOnPageChangeListener(this);
            }
            break;
        }
    }

    return mPager.dispatchTouchEvent(evt);
}

If anyone stumbles on this question and is looking for a solution, I ended up using HorizontalGridView instead of ViewPager... 如果有人在这个问题上迷迷糊糊并正在寻找解决方案,我最终使用了Horizo​​ntalGridView而不是ViewPager ...

Javadoc for HorizontalGridView here Javadoc for Horizo​​ntalGridView在这里

HorizontalGridView Sample Horizo​​ntalGridView示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM