未触发 ViewPager 上的 onClick

onClick on ViewPager not triggered(未触发 ViewPager 上的 onClick)
本文介绍了未触发 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)