我在自定义listview中有两个textviews。当我选择一个列表项时,我希望对选定的行可见holder.text2,而当我选择另一行时,我希望前一个holder.text2不可见并且新的选定位置行holder.text2变得可见。我怎样才能做到这一点?。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResID, parent, false);
holder = new ViewHolder();
holder.text1 = (TextView) row.findViewById(R.id.text1);
holder.text2 = (TextView) row.findViewById(R.id.text2);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
Item item = items.get(position);
holder.text1.setText(item.gettext());
holder.text2.setText(item.getText2());
return row;
}
public static class ViewHolder {
TextView text1, text2;
}
当项目单击时:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ViewHolder holder= (ViewHolder) view.getTag();
holder.text2.setVisibility(View.VISIBLE);
}
最佳答案
在列表项持有者中添加一个布尔值falg,然后尝试更改项目click中的flag状态:
public class Item {
private String text1;
private String text2;
private boolean isSelected;
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
现在尝试更改项目click上的标志状态:
ArrayList listItems;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
for (Item item :listItems){
item.setSelected(false);
}
listItems.get(position).setSelected(true);
adapter.notifyDataSetChanged();
}
显示/隐藏text2基本项目选择标志值:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(layoutResID, parent, false);
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(items.get(position).getText1());
holder.text2.setText(ititems.get(position)getText2());
if(items.get(position).isSelected()){
holder.text2.setVisibility(View.VISIBLE)
}else{
holder.text2.setVisibility(View.GONE)
}
return convertView;
}
关于android - 在itemclick的自定义 ListView 中显示隐藏文本 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26901596/