Android的ListView控件有两个按钮设置可见性问题 [英] Android ListView with two buttons set visibility issue
问题描述
I have Drag Sort Listview
with following items.
1)TextView的结果
2)两个按钮(ON和OFF,每次只有一个按钮是可见的)
1) TextView
2) Two buttons (ON and OFF, At a time only one button is visible)
我的问题是,在ON当用户点击和滚动列表视图,回来该项目,也不会更改为开。
My issue is, when user click on ON, and scroll the listview and come back to that item, it will not change to ON.
我试过:
public class Item {
public String title;
boolean selected = false;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
在getView()方法:
Inside getView() method :
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;
}
请帮忙。
推荐答案
只要你需要保持ON或OFF的选中状态,只需保存你的按钮的位置,并把它拿来和公正的保存状态,以便了解详情可以看到的ListView的回收是如何工作的。也为工作演示例如,您可以检查我的博客里面鼠-A-TAT-A-达料理鼠王指出在这个答案。
Simply you need to maintain the selected state of ON or OFF, just save the position of your Button and fetch it and just save the state for more details you can see how recycling of ListView works. Also for a working demo example you can check my blog which Rat-a-tat-a-tat Ratatouille has pointed out in this answer.
我只显示了 btnoff
按钮同样的方式,你可以做到这一点其他还有
I am only showing for btnoff
Button same way you can do it for other as well
的伪code
在code会是这样的,
Psuedo code
in your code would be something like,
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);
}
这篇关于Android的ListView控件有两个按钮设置可见性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!