本文介绍了未触发 ViewPager 上的 onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ViewPager 上设置了一个点击监听器,但从未调用过 onClick 事件.我猜 ViewPager 的触摸事件检测有干扰,但我不知道如何解决它...

I set a click listener on a ViewPager, but the onClick event is never called. I guess the touch event detection of the ViewPager is interfering, but I can't see how to solve it...

有人可以帮忙吗?

谢谢

mViewPager.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // never called
    }
});

推荐答案

确实是viewpager 干扰了.但是您可以覆盖 viewpager 的方法以使其执行您想要的操作.您需要覆盖 ViewGroup.onInterceptTouchEvent(MotionEvent ev) 方法.

Indeed the viewpager is interfering. But you can override the methods of the viewpager to make it do what you want. You'll need to override the ViewGroup.onInterceptTouchEvent(MotionEvent ev) method.

您可以始终返回 false 以允许触摸事件通过.我还建议调用 super.onInterceptTouchEvent(ev) 以允许滑动继续工作.

You could just always return false to allow the touch events to fall through. I would also recommend calling super.onInterceptTouchEvent(ev) to allow swipes to keep working.

它传入一个 MotionEvent,因此如果需要,您可以使用它来检查点击.

It passes in a MotionEvent, so you could check for clicks with that if you wanted.

希望对您有所帮助.它至少应该让你开始.有问题或其他问题回帖.

Hope that helps. It should at least get you started. Post back with questions or further problems.

另请注意,ViewPager 不会消耗点击次数.因此,如果您想捕获点击并可能为自己节省大量工作,您可以轻松地在 viewpager 的任何或所有子级上设置 onclicklistener.

Note also that the ViewPager doesn't consume clicks. Therefore you could easily set an onclicklistener on any or all of the children of the viewpager if you wanted to capture clicks and potentially save yourself a lot of work.

这篇关于未触发 ViewPager 上的 onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:24