onTouch()没有调用父布局

时间:2017-06-14 08:54:43

标签: 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 :))

2 个答案:

答案 0 :(得分:2)

最有可能FrameLayout消耗触摸事件。一旦任何视图使用了触摸事件,该事件就不会被分派给父母。如果FrameLayout附加了点击监听器,则MotionEvent无法到达RelativeLayout

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

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

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

答案 1 :(得分:0)

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