【问题标题】:OnTouch method consumes click event and prevents execution in parentOnTouch 方法消耗点击事件并阻止在父级中执行
【发布时间】:2012-10-14 03:33:44
【问题描述】:

我有一个FrameLayout,小时候有一个extended ImageView (github)。当我将onClick()-Event 设置为FrameLayout 时,它不会被触发。原因似乎是onTouch() method 的返回值。

如果我将 ACTION_DOWN 的返回值设置为 false,则事件会正确传递 - 但是多点触控功能会中断。在 ACTION_UP 事件中运行 performClick() 也一无所获。

如何正确处理这些事件?

【问题讨论】:

    标签: android android-view ontouchlistener android-event


    【解决方案1】:

    目前我通过手动将点击事件传递给视图的父级解决了这个问题:

    new GestureDetector( context, new GestureDetector.SimpleOnGestureListener()
    {
        @Override
        public boolean onSingleTapConfirmed( MotionEvent event )
        {
            View view = MultitouchImageView.this;
    
            if( !performClick() )
            {
                while( view.getParent() instanceof View )
                {
                    view = (View) view.getParent();
    
                    if( view.performClick() )
                    {
                        return true;
                    }
                }
    
                return false;
            }
    
            return true;
        }
    
        ... 
    }
    

    对发生的 LongPress 事件执行相同的操作。它按我需要的方式工作,但我不喜欢这个解决方案..

    【讨论】:

      【解决方案2】:

      尝试将父 FrameLayout 设置为在 ImageView 子之前接收触摸。 您可以通过添加来做到这一点

      android:descendantFocusability="beforeDescendants"
      

      到父级的 xml。

      例如

      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:descendantFocusability="beforeDescendants">
          <ImageView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
      </FrameLayout>
      

      【讨论】:

        猜你喜欢
        • 2017-12-28
        • 1970-01-01
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多