Android ListView 和 ListView 中的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3045872/
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 and Buttons inside ListView
提问by chrisonline
I want to display a Button
inside the ListView
.
The goal should be to click on the ListView
line or on the button.
我想Button
在ListView
. 目标应该是点击ListView
线条或按钮。
Click on the line it shows more info. Click on the button it shows at the bottom more buttons.
单击显示更多信息的行。单击它在底部显示更多按钮的按钮。
The same as the GMAIL app.
与 GMAIL 应用程序相同。
On the right there is a checkbox and after clicking on the checkbox at the bottom, the button bar appears.
右侧有一个复选框,单击底部的复选框后,会出现按钮栏。
My problem is after inserting the button into the ListView
, the button is not clickable.
If I add the to the LinearLayout
from the button llButton.setClickable()
it works. But, only the button. The ListView
itself doesn't react on clicks anymore!
我的问题是将按钮插入 后ListView
,按钮不可点击。如果我LinearLayout
从按钮中添加llButton.setClickable()
它,它就可以工作。但是,只有按钮。在ListView
本身不上点击反应了!
I have tried this example.
我试过这个例子。
The same issue as above...
和楼上一样的问题。。。
采纳答案by Venky
If you are using a custom Adapter the Button click inside a ListView will not work so you should try to use the following code to check for OnItemClickListener
.
如果您使用的是自定义适配器,则在 ListView 中单击按钮将不起作用,因此您应该尝试使用以下代码来检查OnItemClickListener
.
listId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// Your code for item clicks
}
});
回答by flagoworld
Just to make this clear – and no one seems to have said something this simple – whilst one is not allowed to have a focusable button work in conjunction with the list view, there is a much simpler solution for this.
只是为了说明这一点——似乎没有人说过这么简单的事情——虽然不允许将可聚焦按钮与列表视图结合使用,但有一个更简单的解决方案。
The accepted answer is a given - you should always do that when setting the click listener for list items, so that is silly that OP didn't know that.
接受的答案是给定的 - 在为列表项设置点击侦听器时,您应该始终这样做,因此 OP 不知道这一点很愚蠢。
If you are using an XML layout as your list item, simply set the button to have the following attribute and it will cause the list item to be clickable as well:
如果您使用 XML 布局作为列表项,只需将按钮设置为具有以下属性,它就会使列表项也可点击:
android:focusable="false"
android:focusable="false"
回答by Faruk Toptas
Add the line below to your list item XML.
将下面的行添加到您的列表项 XML。
android:descendantFocusability="blocksDescendants"
Then your list item will be like this:
那么你的列表项将是这样的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content" >
// Your layout objects here
</RelativeLayout>
回答by beta
To have the event be triggered when either the button or the list item is clicked, you can do the following:
要在单击按钮或列表项时触发事件,您可以执行以下操作:
You handle only onItemClick:
您只处理 onItemClick:
mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
// handle click here
}
);
In the adapter you modify the button to not be clickable/focusable (or do this in the xml file instead):
在适配器中,您将按钮修改为不可点击/可聚焦(或改为在 xml 文件中执行此操作):
public class MyAdapter extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
.....
Button btn = view.findViewById(R.id.button);
btn.setFocusable(false);
btn.setClickable(false);
}
}
回答by Kokusho
In my case i had to add this attribute in the listView :
就我而言,我必须在 listView 中添加此属性:
<ListView
...
android:clickable="true"
...
</ListView>
And in the adapter just add on click listener in the button view.
在适配器中,只需在按钮视图中添加单击侦听器。
wrapper.getButtonHi().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DebugUtils.logDebug("Clickeado :: "+ mContact.getUserId());
}
});
Its important to set final the variables:
设置 final 变量很重要:
public View getRowView(final int position, View convertView, ViewGroup parent) {
final BrowseContactItemWrapper wrapper;
final UserModel mContact = lstContact.get(position);
.....
}