为填充按钮的线条布局添加触摸监听器

Eva*_*ovi 4 button touch-event android-linearlayout swipe-gesture findviewbyid

我在线性布局中添加了触摸事件以响应滑动手势,并且效果很好.但是,当我向布局添加按钮时,将忽略父线框布局.我应该如何防止这种情况发生?

LinearLayout ln2 = (LinearLayout) findViewById(R.id.fr2);
ln2.setOnTouchListener(swipe);
Run Code Online (Sandbox Code Playgroud)

我该怎么用onInterceptTouch

Mur*_*fiz 10

您应该创建自己的布局并覆盖布局的onInterceptTouchEvent(MotionEvent ev)方法.

例如,我创建了自己的布局,扩展了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;
    }
Run Code Online (Sandbox Code Playgroud)

当我在我的布局中放置一个按钮时,即使我点击了该按钮,我的布局也会消耗掉所有的touchEvent,因为它onInterceptTouchEvent总是返回true.

希望这对你有帮助