当 Items 布局具有可点击/可编辑小部件(RadioButton、EditText 或 CheckBox)时,是否可以在 ListView 上使用 OnItemClickListener?
6 回答
在 Samuh 提到的链接中引用评论 #31(它为我解决了这个问题):
事实上,你可以将它添加到布局 XML 中(如果增加了一个):android:descendantFocusability="blocksDescendants"。
在此处添加 JIC,该网页将来会关闭。
如果列表的任何行项包含可聚焦或可点击的视图,OnItemClickListener
则将不起作用。
行项目必须具有类似的参数android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
尝试了许多复杂的解决方案,但这是最简单的解决方案:
只需使用android:focusable="false"
如下:
<CheckBox
android:id="@+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
两个最佳解决方案
- 添加
android:descendantFocusability="beforeDescendants"
到 xml 中的 listView或 - 将给定的两个属性设置为
false
喜欢
android:focusable="false"
android:focusableInTouchMode="false"
然后它将处理 listView 行项子(按钮,EditText 等)事件,而不是 listView.setOnItemClick 。
我以不同的方式解决了我的问题,在我的项目中,我有多个 LinearLayout,因此如果您在适配器类中为您的 linearayout 和 setOnclickListener 提供 id 它将起作用,只有触摸的原始效果会消失。但是此链接使 LinearLayout 像按钮一样 操作对于使 linearlaout 在单击时像按钮一样有用
物品
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/txt_item_followers_name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center|start"
android:paddingLeft="15dp"
android:text="Ali"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/txt_item_followers_name"
android:layout_marginLeft="10dp"
android:src="@drawable/puan_icon" />
<TextView
android:id="@+id/txt_item_followers_mark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView"
android:layout_toEndOf="@+id/imageView"
android:background="@color/red_400"
android:paddingLeft="10dp"
android:text="25.5"
android:textAppearance="?android:attr/textAppearanceSmall" />
<LinearLayout
android:id="@+id/linear_one"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/txt_item_followers_name"
android:background="@color/red_400"
android:orientation="vertical">
<ImageView
android:id="@+id/btn_item_followers_2b_follow"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_marginLeft="10dp"
android:src="@drawable/follow_buton" />
</LinearLayout>
</RelativeLayout>
在getView方法里面
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
if (convertView == null)
view = inflater.inflate(R.layout.deneme, null);
final Followers2 myObj = myList.get(position);
LinearLayout linear_one = (LinearLayout) view.findViewById(R.id.linear_one); // HERE WE DOMUNİCATE IT
linear_one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(parentActivity, "One Two", Toast.LENGTH_SHORT).show();
}
});
TextView name = (TextView) view.findViewById(R.id.txt_item_followers_name);
TextView mark = (TextView) view.findViewById(R.id.txt_item_followers_mark);
final ImageView btn_follow = (ImageView) view.findViewById(R.id.btn_item_followers_2b_follow);
name.setText(myObj.getName());
mark.setText(myObj.getScore());
/* if (myObj.isFollow() == true) {
btn_follow.setImageResource(R.drawable.following_buton);
} else {
btn_follow.setImageResource(R.drawable.follow_buton);
}
btn_follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Followers2 myObj = myList.get(position);
if (myObj.isFollow() == true) {
btn_follow.setImageResource(R.drawable.following_buton);
} else {
btn_follow.setImageResource(R.drawable.follow_buton);
}
}
});*/
return view;
}