【发布时间】:2017-04-07 20:25:51
【问题描述】:
作为一个初学者,我正在尝试为ListView的父布局LinearLayout设置点击事件。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>
我使用这个代码,但它不起作用。
findViewById(R.id.ll).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("MainActivity", "click");
}
});
【问题讨论】:
-
linearLayout = (LinearLayout) findViewById(R.id.EmailTextView); linearLayout.setOnClickLister(new View.OnClickListener() { @Override public void onClick(View view) { Log.d("MainActivity", "click"); } });
-
@AkshayShinde,这不起作用,因为它不会改变任何东西。是否将视图转换为布局并不重要。
-
尝试 setVisibility(GONE) 到列表视图。然后给线性布局一些高度。那就试试吧。
-
将此用于LinearLayout
android:descendantFocusability(blocksDescendants); -
尝试删除重量并将列表视图的宽度和高度设置为 match_parent。
标签: android android-layout listview