Android 未为父布局调用onTouch()

Android 未为父布局调用onTouch(),android,android-layout,motionevent,android-touch-event,view-hierarchy,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.setOnTouchLi

我有一个这样的布局:

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
谢谢


解决方案:


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

我认为你的相对论被框架覆盖了,但这就是为什么触摸不起作用。
发布布局xml代码

最有可能的是
FrameLayout
使用触摸事件。一旦触摸事件被任何视图使用,该事件将不会发送到父视图。如果
FrameLayout
附加了单击侦听器,则
MotionEvent
将无法到达
RelativeLayout

您可以将
RelativeLayout
子类化,覆盖并跟踪将分派给该布局的子级的
MotionEvent
(在您的示例中是
FrameLayout

在这种情况下,您的视图层次结构将如下所示:

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


我使用@Override public boolean dispatchTouchEvent(MotionEvent ev){return false;}进行了自定义布局,但它不起作用,只能帮助您调试
MotionEvent
s并查看发生了什么,它不应该在任何方面帮助您。此外,返回
false
将不允许将
MotionEvent
分派给子级,相反,返回
true
,以便让
FrameLayout
处理点击。当我触摸屏幕时,CustomLayouts调度将运行。非常感谢您,CustomLayout解决方案工作正常,但是如果
dispatchTouchEvent
返回false,
FragmeLayout
无法处理触摸事件。在我的场景
FragmeLayout
中包含片段,因此我将视图滑动到
FrameLayout
上,片段无法处理触摸事件。
Touch 
-> Activity:dispatchTouchEvent 
-> LinearLayout:dispatchTouchEvent 
-> CustomRelativeLayout:dispatchTouchEvent 
-> CustomRelativeLayout:onTouch
-> LinearLayout:onTouch
-> Activity:onTouch (IT WORK :))
<LinearLayout>
    <com.example.CustomRelativeLayout>
        <FrameLayout>
    </com.example.CustomRelativeLayout>
</LinearLayout>