Android 如何使用列表中的按钮在 Listactivity 中触发 onListItemClick?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1821871/
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
How to fire onListItemClick in Listactivity with buttons in list?
提问by CodeFusionMobile
I have a simple ListActivity that uses a custom ListAdapter to generate the views in the list. Normally the ListAdapter would just fill the views with TextViews, but now I want to put a button there as well.
我有一个简单的 ListActivity,它使用自定义 ListAdapter 生成列表中的视图。通常 ListAdapter 只会用 TextViews 填充视图,但现在我也想在那里放一个按钮。
It is my understanding and experience however that putting a focusable view in the list item prevents the firing of onListItemClick() in the ListActivity when the list item is clicked. The button still functions normally within the list item, but when something besides the button is pressed, I want onListItemClick to be triggered.
但是,根据我的理解和经验,在列表项中放置可聚焦视图可以防止在单击列表项时触发 ListActivity 中的 onListItemClick()。该按钮仍然在列表项中正常运行,但是当按下按钮之外的其他东西时,我希望 onListItemClick 被触发。
How can I make this work?
我怎样才能使这项工作?
回答by Ewoks
as I wrote in previous commentsolution is to setFocusable(false) on ImageButton.
正如我在之前的评论中所写,解决方案是在 ImageButton 上设置Focusable(false)。
There is even more elegant solution try to add android:descendantFocusability="blocksDescendants"
in root layoutof list element. That will make clicks onListItem possible and separately u can handle Button or ImageButton clicks
有更优雅的解决方案尝试添加列表元素的android:descendantFocusability="blocksDescendants"
根布局。这将使点击 onListItem 成为可能,并且您可以单独处理 Button 或 ImageButton 点击
Hope it helps ;)
希望能帮助到你 ;)
Cheers
干杯
回答by Ramps
I hope I can help here. I assume that you have custom layout for listView items, and this layout consists of button and some other views - like TextView, ImageView or whatever. Now you want to have different event fired on button click and different event fired on everything else clicked.
You can achieve that without using onListItemClick() of your ListActivity.
Here is what you have to do:
我希望我能在这里提供帮助。我假设你有 listView 项目的自定义布局,这个布局由按钮和其他一些视图组成 - 比如 TextView、ImageView 或其他什么。现在,您希望在单击按钮时触发不同的事件,并在单击其他所有内容时触发不同的事件。
您可以在不使用 ListActivity 的 onListItemClick() 的情况下实现这一点。
这是你必须做的:
You are using custom layout, so probably you are overriding getView() method from your custom adapter. The trick is to set the different listeners for your button and different for the whole view (your row). Take a look at the example:
您正在使用自定义布局,因此您可能正在覆盖自定义适配器中的 getView() 方法。诀窍是为您的按钮设置不同的侦听器,并为整个视图(您的行)设置不同的侦听器。看一个例子:
private class MyAdapter extends ArrayAdapter<String> implements OnClickListener {
public MyAdapter(Context context, int resource, int textViewResourceId,
List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String text = getItem(position);
if (null == convertView) {
convertView = mInflater.inflate(R.layout.custom_row, null);
}
//take the Button and set listener. It will be invoked when you click the button.
Button btn = (Button) convertView.findViewById(R.id.button);
btn.setOnClickListener(this);
//set the text... not important
TextView tv = (TextView) convertView.findViewById(R.id.text);
tv.setText(text);
//!!! and this is the most important part: you are settin listener for the whole row
convertView.setOnClickListener(new OnItemClickListener(position));
return convertView;
}
@Override
public void onClick(View v) {
Log.v(TAG, "Row button clicked");
}
}
Your OnItemClickListener class could be declared like here:
您的 OnItemClickListener 类可以像这样声明:
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
Log.v(TAG, "onItemClick at position" + mPosition);
}
}
Of course you will probably add some more parameters to OnItemClickListener constructor.
And one important thing - implementation of getView shown above is pretty ugly, normally you should use ViewHolder pattern to avoid findViewById calls.. but you probably already know that.
My custom_row.xml file is RelativeLayout with Button of id "button", TextView of id "text" and ImageView of id "image" - just to make things clear.
Regards!
当然,您可能会向 OnItemClickListener 构造函数添加更多参数。
还有一件重要的事情 - 上面显示的 getView 的实现非常难看,通常你应该使用 ViewHolder 模式来避免 findViewById 调用......但你可能已经知道了。
我的custom_row.xml 文件是RelativeLayout,带有id 为“button”的Button,id 为“text”的TextView 和id 为“image”的ImageView - 只是为了让事情清楚。
问候!
回答by Rabi
When a custom ListView contains focusable elements, onListItemClick won't work (I think it's the expected behavior). Just remove the focus from the custom view, it will do the trick:
当自定义 ListView 包含可聚焦元素时, onListItemClick 将不起作用(我认为这是预期的行为)。只需从自定义视图中移除焦点,它就可以解决问题:
For example:
例如:
public class ExtendedCheckBoxListView extends LinearLayout {
private TextView mText;
private CheckBox mCheckBox;
public ExtendedCheckBoxListView(Context context, ExtendedCheckBox aCheckBoxifiedText) {
super(context);
…
mText.setFocusable(false);
mText.setFocusableInTouchMode(false);
mCheckBox.setFocusable(false);
mCheckBox.setFocusableInTouchMode(false);
…
}
}
回答by rfellons
I have the same problem: OnListItemClick not fired ! [SOLVED]
That's happen on class that extend ListActivity,
with a layout for ListActivity that content TextBox and ListView nested into LinearLayout
and another layout for the rows (a CheckBox and TextBox nested into LineraLayout).
我有同样的问题: OnListItemClick 没有被解雇![已解决]
这发生在扩展 ListActivity 的类上,ListActivity
的布局将内容 TextBox 和 ListView 嵌套到 LinearLayout
和另一个行布局(嵌套在 LineraLayout 中的 CheckBox 和 TextBox)。
That's code:
那是代码:
res/layout/configpage.xml(main for ListActivity)
res/layout/configpage.xml(主要用于ListActivity)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="pippo" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:background="#aaFFaa" >
</ListView>
<LinearLayout>
res/layout/row.xml (layout for single row)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:focusable="false"**
**android:focusableInTouchMode="false"** />
<TextView
android:id="@+id/testo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:focusable="false"**
**android:focusableInTouchMode="false"** />
</LinearLayout>
src/.../.../ConfigPage.java
src/.../.../ ConfigPage.java
public class ConfigPage extends ListActivity
{
TextView selection;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configpage);
// loaded from res/value/strings
String[] azioni = getResources().getStringArray(R.array.ACTIONS);
setListAdapter(new ArrayAdapter<String>(this, R.layout.row,
R.id.testo, azioni));
selection = (TextView) findViewById(R.id.selection);
}
public void onListItemClick(ListView parent, View view, int position, long id)
{
selection.setText(" " + position);
}
}
This begin to work when I added on row.xml
当我添加 row.xml 时,这开始工作
android:focusable="false"
android:focusableInTouchMode="false"
android:focusable="false"
android:focusableInTouchMode="false"
I use Eclipse 3.5.2
Android SDK 10.0.1
min SDK version: 3
我使用 Eclipse 3.5.2
Android SDK 10.0.1
min SDK 版本:3
I hope this is helpful
... and sorry for my english :(
我希望这会有所帮助
......并且为我的英语感到抱歉:(
回答by John
just add android:focusable="false"
as one of the attributes of your button
只需添加android:focusable="false"
为按钮的属性之一
回答by confucius
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
回答by Ridiculous
I've had the same problem with ToggleButton. After half a day of banging my head against a wall I finally solved it. It's as simple as making the focusable view un-focusable, using 'android:focusable'. You should also avoid playing with the focusability and clickability (I just made up words) of the list row, just leave them with the default value.
我在 ToggleButton 上遇到了同样的问题。在我的头撞墙半天后,我终于解决了它。就像使用“android:focusable”使可聚焦视图不可聚焦一样简单。您还应该避免玩弄列表行的可聚焦性和可点击性(我只是编造了单词),只需将它们保留为默认值即可。
Of course, now that your focusable views in the list row are un-focusable, users using the keyboard might have problems, well, focusing them. It's not likely to be a problem, but just in case you want to write 100% flawless apps, you could use the onItemSelected event to make the elements of the selected row focusable and the elements of the previously selected row un-focusable.
当然,既然列表行中的可聚焦视图不可聚焦,使用键盘的用户可能会遇到问题,好吧,聚焦它们。这不太可能成为问题,但如果您想编写 100% 完美的应用程序,您可以使用 onItemSelected 事件使所选行的元素可聚焦,而先前所选行的元素不可聚焦。
回答by Mikey
I used the getListAdapter().getItem(position) instantiating an Object that holds my values within the item
我使用 getListAdapter().getItem(position) 实例化一个对象,该对象在项目中保存我的值
MyPojo myPojo = getListAdapter().getItem(position);
then used the getter method from the myPojo it will call its proper values within the item .
然后使用 myPojo 中的 getter 方法,它将在 item 中调用其正确的值。