安卓:ListFragment获得点击之内的ListItem项之内、ListFragment、ListItem

2023-09-07 13:25:23 作者:无所谓的魅惑

我的应用程序包含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禁用点击。如果你不知道怎么回事,检查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;
}