Android 为什么在充气布局上附加触控侦听器不会注册触控事件?
所以我在开发我的Android应用程序时遇到了一个有点古怪的行为,我很好奇为什么会这样。执行此操作时,将注册以下非接触事件:Android 为什么在充气布局上附加触控侦听器不会注册触控事件?,android,android-layout,Android,Android Layout,所以我在开发我的Android应用程序时遇到了一个有点古怪的行为,我很好奇为什么会这样。执行此操作时,将注册以下非接触事件: mInflatedLayout = getLayoutInflater().inflate(R.layout.my_layout, null, false); mInflatedLayout.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent e
mInflatedLayout = getLayoutInflater().inflate(R.layout.my_layout, null, false);
mInflatedLayout.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.e(TAG, "Hello world");
return false;
}
});
mInflatedLayout = getLayoutInflater().inflate(R.layout.my_layout, null, false);
mInflatedLayout.findViewById(R.id.child_view)
.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.e(TAG, "Hello world");
return false;
}
});
但是,当我执行以下操作时,我会注册触摸事件:
mInflatedLayout = getLayoutInflater().inflate(R.layout.my_layout, null, false);
mInflatedLayout.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.e(TAG, "Hello world");
return false;
}
});
mInflatedLayout = getLayoutInflater().inflate(R.layout.my_layout, null, false);
mInflatedLayout.findViewById(R.id.child_view)
.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.e(TAG, "Hello world");
return false;
}
});
注意:为了简单起见,上面的代码是对我实际应用的伪代码重写。请随意忽略任何可能导致编译时错误的内容
我的问题是,为什么会发生这种情况?我很愿意猜测这里发生了一些非常简单的事情,但我对这是什么感到有点困惑
编辑:这是我正在使用的布局文件。值得一提的是,视图本身显示为ListView的空视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/header_view" />
<LinearLayout
android:id="@+id/child_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingRight="?baseGapSize"
android:paddingLeft="?baseGapSize"
android:orientation="vertical" >
<CustomFrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_white_rounded_rect_inactive"
android:paddingBottom="?baseGapSize"
android:paddingTop="?baseGapSize" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="@style/StandardLightText" />
</CustomFrameLayout>
<CustomRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_white_rounded_rect_inactive" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@string/take_a_photo"
android:paddingBottom="?baseGapSize"
android:paddingTop="?baseGapSize"
android:src="@drawable/camera_with_plus" />
</CustomRelativeLayout>
</LinearLayout>
您的布局可能未暴露-整个屏幕可能被子视图占据,因此其后面的布局永远无法触摸。。或 你可能有
android:clickable="true" //in childview
或
正在导致您的布局,而不是触发触摸事件。尝试删除此项。可能您的
子视图
覆盖了父视图,也“吃”了触摸事件?但是如果我没有在子视图
上定义触摸监听器,它会不会将触摸事件传递给父视图?在某些情况下不会(如EditText,TextView)。你可能想发布你的布局文件。我已经在上面发布了我的布局以及上面的更多信息。感谢您提供的任何见解。这两个都没有被调用。即使是这样,为什么一个线性布局上的触摸事件不会上升到其父布局?