Android:ListFragment获取ListItem中的单击项

时间:2014-06-11 18:40:10

标签: android android-listview android-listfragment

我的应用包含3个标签。所有标签都包含List,所以我使用了ListFragment。每个ListItem中都有一个按钮。我想做" 某事"当" 点击"在ListItem中单击按钮,如图所示。

ListFragment 我该如何实现呢。关于为ListActivity做同样的事情,但没有 ListFragment .Thanks提前。 :)

1 个答案:

答案 0 :(得分:3)

首先,您需要为ListView实现自定义适配器。如果您不熟悉,请阅读this article

接下来,您必须禁用ClickView项目的单击。如果您不知道如何操作,请检查this

现在,在自定义适配器的getView方法中,您可以找到按钮并设置onClickListener,但只有在为当前ListView位置充气后才能显示。

@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;
}