单击按钮将更改列表项中按钮的可见性,从不可见到可见

时间:2012-10-11 12:26:46

标签: android android-listview

enter image description here

在列表项目中,有一个按钮“删除”和textview ..点击列表项中的删除按钮我正在删除该特定列表项..但我的问题是:删除按钮在开始时是不可见的当用户点击屏幕顶部下方显示的“编辑”按钮时,我想让所有按钮从不可见到可见..但我没有得到如何实现...

ListViewAdapater.java:

public class ListViewAdapter extends BaseAdapter {

    public ArrayList<String> list;
    Context context;
    private LayoutInflater mInflater;

    int count = 0;
    static int list_size;

    public ListViewAdapter(Context context, ArrayList<String> list) {
        super();
        this.context = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
        list_size = list.size();
    }

    public int getCount() {
        if (list == null) {
            return 0;
        } else {
            return list_size;
        }
    }

    @Override
    public Object getItem(int position) {
        Log.i("Calling getItem()", "getItem()");
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        Log.i("Calling getItemId()", "getItemId()");
        return position;
    }

    static class ViewHolder {
        TextView notes_text;

        static Button delete;
    }

    //  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("ListViewAdapter is calling...", "calling...");
        ViewHolder holder;
        Log.i("View to be converted", "" + convertView);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.simplerow, null);
            Log.i("View to be converted", "" + convertView);
            holder = new ViewHolder();
            holder.notes_text = (TextView) convertView.findViewById(R.id.rowTextView);

            holder.delete = (Button) convertView.findViewById(R.id.btn_minus);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.notes_text.setText((String) list.get(position));

        holder.delete.setTag((Integer) position);

        holder.delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Integer myIntegerObject = new Integer((Integer) view.getTag());
                Log.i("Integer postioton", "" + myIntegerObject.intValue());

                list.remove(list.indexOf(list.get(myIntegerObject.intValue())));

                list_size = list.size();

                notifyDataSetChanged();
            }
        });

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:4)

保留一个布尔标志以指示编辑模式。在ListAdapter getView() Button设置VISIBLE GONEnotifyDataSetChanged()的可见性,具体取决于您的布尔标记。

现在,要切换所有按钮可见性,请更改布尔标志,并在适配器上调用boolean editmode = false; 。这将重新绘制所有可见的列表项。

在适配器类中添加变量:

getView()

holder.delete.setVisibility( (editmode ? View.VISIBLE : View.GONE ) );

public void setEditMode(boolean value){
  editmode = value;
  this.notifyDataSetChanged();
}

最后在适配器类中添加一个方法:

{{1}}