加入触摸监听器衬垫布局充满了按钮 [英] adding touch listener for liner layout filled with buttons

查看:319
本文介绍了加入触摸监听器衬垫布局充满了按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我增加了一个触摸事件线性布局,以应对滑动手势,而且效果很好。然而,当我添加按钮的布局,父衬垫布局被忽略。我应该如何prevent这种情况的发生?

I added a touch event to a linear layout to respond to swipe gestures, and it works well. However, when i add buttons to the layout, the parent liner layout is ignored. How should I prevent this from happening?

LinearLayout ln2 = (LinearLayout) findViewById(R.id.fr2);
ln2.setOnTouchListener(swipe);

如何我使用 onInterceptTouch

推荐答案

您应该创建自己的布局,并覆盖onInterceptTouchEvent(MotionEvent EV)布局的方法。

You should create your own layout and override onInterceptTouchEvent(MotionEvent ev) method of your layout.

例如我创建了自己的布局延伸的RelativeLayout

For Example I created my own layout which extends RelativeLayout

       @Override
       public boolean onInterceptTouchEvent(MotionEvent ev) {
         return true; // With this i tell my layout to consume all the touch events from its childs
      }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        // Log.d(TAG, String.format("ACTION_DOWN | x:%s y:%s", 
            break;
        case MotionEvent.ACTION_MOVE:
        //Log.d(TAG, String.format("ACTION_MOVE | x:%s y:%s", 
            break;
        case MotionEvent.ACTION_UP:
            break;
        }
        return true;
    }

当我把一个按钮,我的布局,即使我点击该按钮,我的布局消耗,因为所有的TouchEvent使用 onInterceptTouchEvent 总是返回true。

希望这会帮助你。

这篇关于加入触摸监听器衬垫布局充满了按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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