gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:21:40 26 4
gpt4 key购买 nike

我有Drag Sort Listview包含以下项目。

1) TextView
2)两个按钮(ON和OFF,一次只有一个按钮可见)

Image with OFF state .
Image with ON state .

我的问题是,当用户点击 ON 并滚动 ListView 并返回到该项目时,它不会更改为 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;
}

请帮忙。

最佳答案

简单地说,您需要保持 ON 或 OFF 的选定状态,只需保存 Button 的位置并获取它并保存状态即可了解更多详细信息,您可以查看 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);
}

关于带有两个按钮的 Android ListView 设置可见性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com