的onClick上ViewPager不会触发 [英] onClick on ViewPager not triggered

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

问题描述

我设置一个 ViewPager 点击监听器,但onClick事件不会被调用。我想触摸事件检测 ViewPager 的干扰,但我看不出如何解决呢?

任何人都可以帮助?

感谢

  mViewPager.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        //从不叫
    }
});
 

解决方案

事实上,viewpager被干扰。但是你可以重写viewpager的方法,使其做你想做的。你需要重写<一href="http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29">ViewGroup.onInterceptTouchEvent(MotionEvent EV)方法。

您可以只始终返回false,让触摸事件告吹。我还建议调用super.onInterceptTouchEvent(EV),允许刷卡来继续工作。

它通过在 MotionEvent ,所以你可以点击次数与检查,如果你想

希望有所帮助。它至少应该让你开始。回来后有疑问或进一步的问题。

编辑:

还要注意ViewPager不消耗点击。因此,你可以很容易地在任何viewpager的儿童或全部设置onclicklistener如果你想捕捉的点击,并可能为自己节省大量的工作。

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...

Anybody could help?

Thanks

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

解决方案

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.

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.

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.

EDIT:

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.

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

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