Android: ListFragment get clicked item within ListItem

喜你入骨 提交于 2020-01-24 01:54:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!