繁体   English   中英

为什么在膨胀的布局上附加触摸侦听器不会注册触摸事件?

[英]Why does attaching touch listeners on an inflated layout not register touch events?

因此,在开发Android应用程序时遇到了一些时髦的行为,我很好奇为什么会发生这种情况。 当我执行以下操作时,不会记录任何触摸事件:

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

要么

child_view.setClickable(true);

导致您的布局,而不是触发触摸事件。 尝试删除它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM