android - onTouch() 未调用父布局

标签 android android-layout motionevent android-touch-event view-hierarchy

我有这样的布局:

LinearLayout
    RelativeLayout(id = test)
        FrameLayout (From a LIB. id = lib)

我尝试将 onTouchListener() 添加到 test 但它不起作用。

我试过:

@OnTouch(R.id.test)
public boolean test(View v,MotionEvent e) { NOT WORK }

我尝试了一个简单的监听器:

    test.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
NOT WORK
                    return false;
                }
            });

我尝试将监听器添加到 FrameLayout 但也不起作用。

我不想修改库。 任何人都知道使用 OnTouch 事件吗?

<RelativeLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black"
    android:layout_weight="1">

        <com.flurgle.camerakit.CameraView
            android:id="@+id/camera"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_horizontal"
            android:adjustViewBounds="true"
            app:ckCropOutput="false"
            app:ckFacing="back"
            app:ckFlash="off"
            app:ckFocus="continuous"
            app:ckJpegQuality="100"
            app:ckMethod="standard"/>

    <TextView

在 CameraView 中:

focusMarkerLayout.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent motionEvent) {
            int action = motionEvent.getAction();
            if (motionEvent.getAction() == MotionEvent.ACTION_UP && mFocus == CameraKit.Constants.FOCUS_TAP_WITH_MARKER) {
                focusMarkerLayout.focus(motionEvent.getX(), motionEvent.getY());
            }

            mPreviewImpl.getView().dispatchTouchEvent(motionEvent);
            return true;
        }
    });

谢谢。


解决方案:


LinearLayout
    RelativeLayout(id = test)
        CustomRelativeLayout()
            FrameLayout (From a LIB. id = lib)

创建 CustomRelativeLayout

public class CustomRelativeLayout extends RelativeLayout{
    public CustomRelativeLayout(Context context) {
        super(context);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return false;
    }
}

问题:

Touch 
-> Activity:dispatchTouchEvent 
-> LinearLayout:dispatchTouchEvent 
-> CustomRelativeLayout:dispatchTouchEvent 
-> CameraView:dispatchTouchEvent (return true) STOPPED everything

解决方法:

Touch 
-> Activity:dispatchTouchEvent 
-> LinearLayout:dispatchTouchEvent 
-> CustomRelativeLayout:dispatchTouchEvent 
-> CustomRelativeLayout:onTouch
-> LinearLayout:onTouch
-> Activity:onTouch (IT WORK :))

最佳答案

很可能 FrameLayout 消耗了触摸事件。一旦触摸事件被任何 View 使用,该事件将不会被分派(dispatch)给父级。如果 FrameLayout 附加了点击监听器,则 MotionEvent 将不会到达 RelativeLayout

您可以继承RelativeLayout,覆盖ViewGroup#dispatchTouchEvent(MotionEvent)并跟踪将分派(dispatch)给该布局的子级的 MotionEvent(在您的情况下为 FrameLayout)。

在那种情况下,您的 View 层次结构将是这样的:

<LinearLayout>
    <com.example.CustomRelativeLayout>
        <FrameLayout>
    </com.example.CustomRelativeLayout>
</LinearLayout>

关于android - onTouch() 未调用父布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44540014/

相关文章:

android - 我可以将外部 XML 文件用于 Android 中的布局吗?

java - 将 MotionEvent 值存储在 ArrayList 中

java - 如何仅在特定时间调用函数

android - 如何在android中显示每秒动态变化的当前时间

android - 如何在不使用 adb 的情况下在 android 中安装 android 应用程序?

android - Galaxy Note II - values-xhdpi 还是 values-large?

android - 水平滚动的水平回收器 View

android - 在 Android 中绘制锦标赛支架的最佳方式

安卓布局 : Game Board

android - 尝试实现 2 手指缩放