Android 带有可点击/可编辑小部件的 ListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2098558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ListView with clickable/editable widget
提问by bugzy
Is it possible to use a OnItemClickListener on a ListView when the Items layout has a clickable/editable widget (RadioButton,EditText, or CheckBox)?
当 Items 布局具有可点击/可编辑小部件(RadioButton、EditText 或 CheckBox)时,是否可以在 ListView 上使用 OnItemClickListener?
回答by Samuh
You might want to take a look at this issue. Having a focusable item in a row of a ListView
causes the OnItemClickListener
NOT to be invoked. However, that does not mean you cannot have focusable/clickable items in a row, there are some workarounds like this one.
您可能想看看这个问题。在 a 的一行中有一个可聚焦的项目ListView
会导致OnItemClickListener
NOT 被调用。但是,这并不意味着您不能连续拥有可聚焦/可点击的项目,有一些像这样的解决方法。
Also, you can take a look at the Call Logs screen. It has a ListView
with clickable item(the call icon on the right).
See Source code here
此外,您还可以查看通话记录屏幕。它有一个ListView
可点击的项目(右侧的呼叫图标)。
在此处查看源代码
回答by khalid13
Quoting comment #31 in the link mentioned by Samuh (which solved the problem for me):
在 Samuh 提到的链接中引用 #31 评论(为我解决了问题):
In fact you can add it to the layout XML (if inflated by one): android:descendantFocusability="blocksDescendants".
事实上,您可以将它添加到布局 XML(如果膨胀了一个):android:descendantFocusability="blocksDescendants"。
Adding here JIC that webpage is down in the future.
在此处添加 JIC 该网页将来会关闭。
回答by Bhavesh Hirpara
If any row item of list contains focusable or clickable view then OnItemClickListener
won't work.
如果列表的任何行项目包含可聚焦或可点击的视图,OnItemClickListener
则将不起作用。
row item must be having param like android:descendantFocusability="blocksDescendants"
行项目必须具有类似的参数 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>
回答by Henrique de Sousa
Tried many complex solutions, but this was the simplest one that worked:
尝试了许多复杂的解决方案,但这是最简单的解决方案:
Just use android:focusable="false"
as in:
只需使用android:focusable="false"
如下:
<CheckBox
android:id="@+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
回答by Jawad Zeb
Two best solution
两个最佳解决方案
- Add
android:descendantFocusability="beforeDescendants"
to listView in xml OR - Set given two attributes to
false
- 添加
android:descendantFocusability="beforeDescendants"
到 xml 中的 listView或 - 将给定的两个属性设置为
false
like
喜欢
android:focusable="false"
android:focusableInTouchMode="false"
Then it will handle the listView row item child(Button,EditText etc) events instead of listView.setOnItemClick .
然后它将处理 listView 行项目 child(Button,EditText etc) 事件而不是 listView.setOnItemClick 。
回答by Sam
I fixed my problem different , in my item I have more than one LinearLayout so if you give id to your linearayout and setOnclickListener in adapter class it will work, only original effect of touching will dissapear. but this link Making a LinearLayout act like an Buttonis usefull to make linearlaout act like button on click
我解决了不同的问题,在我的项目中,我有不止一个 LinearLayout,所以如果你在适配器类中为你的线性布局和 setOnclickListener 提供 id 它将起作用,只有触摸的原始效果会消失。但是这个链接使 LinearLayout 像一个按钮对于使线性布局像单击按钮一样有用
item
物品
<?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>
inside getView method
在 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;
}