【发布时间】:2011-01-07 02:24:14
【问题描述】:
当 Items 布局具有可点击/可编辑小部件(RadioButton、EditText 或 CheckBox)时,是否可以在 ListView 上使用 OnItemClickListener?
【问题讨论】:
-
扩展 ListActivity 而不是 Activity 。参考softwarepassion.com/…
当 Items 布局具有可点击/可编辑小部件(RadioButton、EditText 或 CheckBox)时,是否可以在 ListView 上使用 OnItemClickListener?
【问题讨论】:
您可能想看看this issue。在 ListView 的一行中有一个可聚焦的项目会导致 OnItemClickListener 不被调用。但是,这并不意味着您不能连续拥有可聚焦/可点击的项目,有一些解决方法,例如 this one。
此外,您还可以查看通话记录屏幕。它有一个带有可点击项目的ListView(右侧的呼叫图标)。
See Source code here
【讨论】:
android:clickable="true" 之类的操作。那是我的问题。
引用 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 或
false
喜欢
android:focusable="false"
android:focusableInTouchMode="false"
然后它将处理 listView 行项子项(按钮、EditText 等)事件,而不是 listView.setOnItemClick 。
【讨论】:
我以不同的方式解决了我的问题,在我的项目中我有多个 LinearLayout 因此,如果您在适配器类中为您的线性布局和 setOnclickListener 提供 id ,它将起作用,只有触摸的原始效果会消失。 但是这个链接Making a LinearLayout act like an Button对于让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;
}
【讨论】: