Android LinearLayout setOnClickListener不工作

Android LinearLayout setOnClickListener不工作,android,android-layout,listview,Android,Android Layout,Listview,作为初学者,我正在尝试为LinearLayout设置click事件,这是ListView的父布局。它似乎是ListView拦截LinearLayout事件 以下是我的主要布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" an

作为初学者,我正在尝试为LinearLayout设置click事件,这是ListView的父布局。它似乎是ListView拦截LinearLayout事件

以下是我的主要布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll"
android:clickable="true"
tools:context=".MainActivity" >
<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:transcriptMode="alwaysScroll"
    android:divider="@null"
    android:listSelector="@android:color/transparent"
    />
</LinearLayout>

因为上面有ListView。ListView正在获得单击。 如果要单击listView项,请使用

listView.setOnItemClickListener

没错,
ListView
处理点击。你有两个选择

  • 创建
    LinearLayout
    的子类,监听触摸事件,并在需要时拦截它们

  • 不要使用
    LinearLayout
    而使用
    FrameLayout
    并在
    ListView
    之后声明高度/宽度
    匹配父视图的视图,并在该视图上设置click listener

    <FrameLayout>
        <ListView />
        <View 
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
    
    
    

  • linearLayout=(linearLayout)findViewById(R.id.EmailTextView);linearLayout.setOnClickLister(new View.OnClickListener(){@Override public void onClick(视图视图){Log.d(“main活动”,“单击”);})@AkshayShinde,这不会起作用,因为它不会改变任何东西。是否将视图强制转换为布局并不重要。请尝试将可见性(GoE)设置为列表视图。然后为线性布局提供一些高度。然后试一试。将其用于LinearLayout
    android:genderantfocusability(blocksDescendants)尝试移除重量,并将listview的宽度和高度设置为与\u parent匹配。太好了!非常感谢。还有一点,当我使用第二种方法时,我发现FrameLayout中的ListView无法正常工作。如何使它正常工作?
    
    <FrameLayout>
        <ListView />
        <View 
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>