问题
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.
How do I implement this. There are tuts about doing same thing for ListActivity but not ListFragment.Thanks in advance. :)回答1:
First of all, you need to implement a custom adapter for your ListView. Please, read this article if you are not familiar with this.
Next, you have to disable click on ListView items. If you don't know how, check this out.
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;
}
来源:https://stackoverflow.com/questions/24170236/android-listfragment-get-clicked-item-within-listitem