安卓:ListFragment获得点击之内的ListItem项 [英] Android: ListFragment get clicked item within ListItem
问题描述
我的应用程序包含3 tabs.All选项卡包含列表,所以我已经使用ListFragment。有在每个列表项的按钮。我想这样做的东西时,点击按钮被点击的列表项内,如图所示。
My app contains 3 tabs.All the tabs contains List so I have used ListFragment. There is a button in each ListItem. I want to do "something" when "Click" button is clicked within the ListItem as shown in the figure.
我如何实现这一点。大约有做同样的事情ListActivity TUTS但不是 ListFragment .Thanks提前。 :)
How do I implement this. There are tuts about doing same thing for ListActivity but not ListFragment.Thanks in advance. :)
推荐答案
首先,你需要实现你的ListView自定义适配器。请阅读这篇文章如果你不熟悉。
First of all, you need to implement a custom adapter for your ListView. Please, read this article if you are not familiar with this.
接下来,你必须在项目的ListView禁用点击。如果你不知道怎么回事,检查<一href=\"http://stackoverflow.com/questions/13146652/how-to-disable-clicking-on-listview-in-android\">this出。
Next, you have to disable click on ListView items. If you don't know how, check this out.
现在,在你的 getView
方法从自定义接口,你可以找到你的按钮,并设置了一个onClickListener,但你有夸大当前的ListView的观点后,才位置。
Now, in your getView
method from your custom adapter, you can find your Button and set up an onClickListener, but only after you have inflated the view for the current ListView position.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
Button mButton = (Button) view.findViewById(R.id.my_button_id);
mButton.setOnClickListener(mClickListener);
return rowView;
}
这篇关于安卓:ListFragment获得点击之内的ListItem项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!