2

我有这样的布局:

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)

创建 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 :))
4

2 回答 2

4

最有可能FrameLayout消耗触摸事件。一旦触摸事件被任何视图消费,该事件将不会被分派给父级。如果FrameLayout附加了点击侦听器,则MotionEvent不会到达RelativeLayout

您可以子类化RelativeLayout、覆盖ViewGroup#dispatchTouchEvent(MotionEvent)和跟踪MotionEvent将被分派给该布局的子级的 (在您的情况下为FrameLayout)。

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

<LinearLayout>
    <com.example.CustomRelativeLayout>
        <FrameLayout>
    </com.example.CustomRelativeLayout>
</LinearLayout>
于 2017-06-14T09:31:29.680 回答
0

我认为您的 RelativeLayout 已被 FrameLyout 覆盖,这就是触摸不起作用的原因。发布您的布局 xml 代码

于 2017-06-14T09:11:08.170 回答