Android:设置自定义ListView中各个按钮的可见性

时间:2011-11-22 18:10:26

标签: android android-layout android-listview android-ui

我有一个带有自定义适配器的ListView(扩展BaseAdapter),每行的布局由TextView和Button组成。创建列表后,我想更改特定按钮的可见性。

为此,我需要以编程方式访问单个行的视图。我无法找到如何做到这一点。 This answer提到getView(int position),但我找不到那个方法; getView()需要3个参数。我将convertView传递给getView(int position, View convertView, ViewGroup parent)的是什么?

你能指点我正确的方向吗?

更新:View v = myListView.getChildAt(myListView.getFirstVisiblePosition());获取的视图为空。此外,myListView.getChildCount()返回0。

2 个答案:

答案 0 :(得分:1)

onItemClick会将您单击的ListView中的视图传递给您。然后你可以抓住那个视图并随意做任何事情,包括改变可见性等。

http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

答案 1 :(得分:0)

以下是我在列表视图中使用的自定义适配器的示例,其中用户可以在listView中选择项目并将其标记为“删除”。关键是在有视图后添加onClickListener。然后您可以使用它不仅更改视图,还可以更新适配器的数据。 希望您可以修改此代码以适合您的详细信息。

private class DeletePlayerAdapter extends ArrayAdapter<Player> {
    Context context;
    int layoutResourceId;
    ArrayList<Player> data;

    public DeletePlayerAdapter(Context context, int layout,
            ArrayList<Player> list) {
        super(context, layout, list);
        this.layoutResourceId = layout;
        this.context = context;
        this.data = list;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View row = convertView;
        PlayerHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = ((Activity) context)
                    .getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new PlayerHolder();
            holder.player_name = (TextView) row
                    .findViewById(R.id.player_name);
            holder.player_number = (TextView) row
                    .findViewById(R.id.player_number);
            holder.seeded_button = (ImageButton) row
                    .findViewById(R.id.delete_toggle);
            holder.player_name.setTypeface(tf);
            holder.player_number.setTypeface(tf);
            row.setTag(holder);
            players_array.get(position).marked_for_delete = false;

        } else {
            Log.d("PLAYER_ADAPTER", "NOT_NULL ROW");
            holder = (PlayerHolder) row.getTag();
        }
        holder.seeded_button.setOnClickListener(new OnClickListener() {
            private int pos = position;

            public void onClick(View v) {
                ImageButton b = (ImageButton) v;
                if (b.isSelected()) {
                    b.setSelected(false);
                    players_array.get(pos).marked_for_delete = false;
                } else {
                    b.setSelected(true);
                    players_array.get(pos).marked_for_delete = true;
                }
            }
        });
        Player p = data.get(position);
        holder.player_name.setText(p.name);
        holder.player_number.setText(String.valueOf(position+1));
        holder.seeded_button
                .setSelected(players_array.get(position).marked_for_delete);
        return row;
    }

}

static class PlayerHolder {
    TextView player_number;
    TextView player_name;
    ImageButton seeded_button;
}