onTouch() 未为父布局调用



我有一个这样的布局:

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

在相机视图中:

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)

创建自定义相对布局

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使用触摸事件。一旦触摸事件被任何视图使用,该事件将不会调度到父级。如果FrameLayout附加了点击侦听器,则MotionEvent不会到达RelativeLayout

您可以子类化RelativeLayout,覆盖ViewGroup#dispatchTouchEvent(MotionEvent)并跟踪将调度到该布局的子级(在您的情况下为 FrameLayout(的MotionEvent

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

<LinearLayout>
    <com.example.CustomRelativeLayout>
        <FrameLayout>
    </com.example.CustomRelativeLayout>
</LinearLayout>
我认为

你的RelativeLayout被FrameLyout覆盖了,这就是为什么触摸不起作用的原因。发布布局 xml 代码

最新更新