Android ListView有两个按钮设置可见性问题

时间:2014-03-24 06:41:24

标签: android

我有以下项目Drag Sort Listview

1)TextView
  2)两个按钮(ON和OFF,一次只能看到一个按钮)

Image with OFF state
 Image with ON state

我的问题是,当用户点击ON,然后滚动列表视图并返回该项目时,它将不会更改为ON。

我试过了:

public class Item {

    public String title;  
    boolean selected = false;

}
public void setSelected(boolean selected) {
    this.selected = selected;
}

getView()方法内部:

 public View getView(final int position, View convertView,
            ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if (v != convertView && v != null) {
            ViewHolder holder = new ViewHolder();
            TextView tv = (TextView) v.findViewById(R.id.txtsettingname);
            ImageButton btnoff = (ImageButton) v.findViewById(R.id.btnoff);
            ImageButton btnon = (ImageButton) v.findViewById(R.id.btnon);
            holder.title = tv;
            holder.btnoff = btnoff;
            holder.btnon = btnon;
            v.setTag(holder);
        }
        final ViewHolder holder = (ViewHolder) v.getTag();
        String albums = getItem(position).title;
        holder.btnoff.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    ImageButton cb = (ImageButton) v;
                    Item _state = (Item) cb.getTag();
                    _state.setSelected(false);
                    holder.btnon.setVisibility(View.VISIBLE);
                    holder.btnoff.setVisibility(View.GONE);


            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });
    holder.btnon.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                ImageButton cb = (ImageButton) v;
                Item _state = (Item) cb.getTag();
                _state.setSelected(true);

                // TODO Auto-generated method stub
                holder.btnoff.setVisibility(View.VISIBLE);
                holder.btnon.setVisibility(View.GONE);



            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });

    holder.title.setText(albums);
    Item state = myarrraylist.get(position);

    if (state.isSelected()) {
        holder.btnon.setVisibility(View.VISIBLE);
        holder.btnoff.setVisibility(View.GONE);
    } else {
        holder.btnon.setVisibility(View.GONE);
        holder.btnoff.setVisibility(View.VISIBLE);
    }
    holder.btnon.setTag(state);

    return v;
}

请帮忙。

3 个答案:

答案 0 :(得分:1)

您必须维护按钮的状态。一些样本包含在here中。

getView方法中的标记设置为按钮,并在onClick侦听器中获取标记以设置状态。

答案 1 :(得分:1)

只需要保持选定的ON或OFF状态,只需保存按钮的位置并获取它,只需保存状态即可查看更多详细信息how recycling of ListView works。另外,对于一个有效的演示示例,您可以查看 Rat-a-tat-a-tat Ratatouille在此答案中指出的博客。

我只为btnoff按钮显示,同样可以为其他按钮显示

您的代码中的

Psuedo code类似于

public View getView(final int position, View convertView,
            ViewGroup parent) {
           ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            holder.title tv = (TextView) convertView.findViewById(R.id.txtsettingname);
            holder.btnoff btnoff = (ImageButton) convertView.findViewById(R.id.btnoff);
            holder.btnon btnon = (ImageButton) convertView.findViewById(R.id.btnon);

        holder.btnoff.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            int pos = (Integer)v.getTag();
        myarrraylist.get(pos).setSelected(false);
        }
    });

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

        holder.btnoff btnoff.setTag(position);
        holder.btnoff btnon.setTag(position);


    if (myarrraylist.get(position).isSelected()) {
        holder.btnon.setVisibility(View.VISIBLE);
        holder.btnoff.setVisibility(View.GONE);
    } else {
        holder.btnon.setVisibility(View.GONE);
        holder.btnoff.setVisibility(View.VISIBLE);
    }

答案 2 :(得分:0)

myarrraylist.set(position, _state);侦听器中添加onClick,因此您将从全局数组更新按钮的状态。

public View getView(final int position, View convertView,
            ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if (v != convertView && v != null) {
            ViewHolder holder = new ViewHolder();
            TextView tv = (TextView) v.findViewById(R.id.txtsettingname);
            ImageButton btnoff = (ImageButton) v.findViewById(R.id.btnoff);
            ImageButton btnon = (ImageButton) v.findViewById(R.id.btnon);
            holder.title = tv;
            holder.btnoff = btnoff;
            holder.btnon = btnon;
            v.setTag(holder);
        }
        final ViewHolder holder = (ViewHolder) v.getTag();
        String albums = getItem(position).title;
        holder.btnoff.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    ImageButton cb = (ImageButton) v;
                    Item _state = (Item) cb.getTag();
                    _state.setSelected(false);

                    holder.btnon.setVisibility(View.VISIBLE);
                    holder.btnoff.setVisibility(View.GONE);

                    myarrraylist.set(position, _state);

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });
    holder.btnon.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                ImageButton cb = (ImageButton) v;
                Item _state = (Item) cb.getTag();
                _state.setSelected(true);

                holder.btnoff.setVisibility(View.VISIBLE);
                holder.btnon.setVisibility(View.GONE);

                myarrraylist.set(position, _state);

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });

    holder.title.setText(albums);
    Item state = myarrraylist.get(position);

    if (state.isSelected()) {
        holder.btnon.setVisibility(View.VISIBLE);
        holder.btnoff.setVisibility(View.GONE);
    } else {
        holder.btnon.setVisibility(View.GONE);
        holder.btnoff.setVisibility(View.VISIBLE);
    }
    holder.btnon.setTag(state);

    return v;
}