Android Gridview 中的复选框回收 View 问题

标签 android

我在 Gridview 中有几个 TextView、ImageView 和复选框。我的复选框正在工作,我可以选中和取消选中,但问题是这是多选的。如果我选中第一个复选框并向下滚动,我会看到第四个、第七个、第十个项目复选框也被选中。我读了很多书,我相信这是因为回收 View 。我不知道如何解决这个问题。我想要的只是如果我单击第一个和第六个复选框,则只应选中这两个框。下面是我的 BaseAdapter 代码。我是 Android 新手,请帮助我。

public class test extends BaseAdapter {
public int itemSelected = 0;


private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
SparseBooleanArray checked;
HashMap<String, String> compareSelectionList1 = new HashMap<String, String>();

public test(Activity a, ArrayList<HashMap<String, String>> d) {

activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return data.size();
}

@Override
public Object getItem(int position) {
//return position;
return data.get(position);    
}

@Override
public long getItemId(int position) {
return position;
//return data.get(position); 
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
        if (convertView == null) {
        LayoutInflater inflater1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = inflater1.inflate(R.layout.comparison_selection_list_row_del, parent, false);

              holder = new ViewHolder();             
              holder.txtBrandName = (TextView) convertView.findViewById(R.id.txtBrandsn);
              holder.prodImg = (ImageView) convertView.findViewById(R.id.btnSmallImage);
              holder.recFlagImg = (ImageView) convertView.findViewById(R.id.btnRecFlag);

              holder.txtPartNumber = (TextView) convertView.findViewById(R.id.txtpartnumber);
              holder.txtNotes = (TextView) convertView.findViewById(R.id.txtnotes);

              holder.chkBox = (CheckBox) convertView.findViewById(R.id.chkBox);
                             holder.chkBox.setId(position);

              convertView.setTag(holder);

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


compareSelectionList1 = data.get(position);
holder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       if(isChecked) {
        holder.chkBox.setBackgroundResource(R.drawable.compselection_checked_btn);           
       } else {
        holder.chkBox.setBackgroundResource(R.drawable.compselection_btn);           
       }
   }
});

holder.txtBrandName.setText(compareSelectionList1.get("brands"));


holder.txtPartNumber.setText(compareSelectionList1.get("partnumbers"));
holder.txtNotes.setText(compareSelectionList1.get("footnotes"));
holder.txtNotes.setClickable(true);
holder.txtPartNumber.setClickable(true);
holder.id = position;

return convertView;   
}

}

"); 

最佳答案

您还必须从 holder 设置复选框的选中状态。否则,检查状态将是回收 View 的状态。

我必须说,你实现这一切有点笨拙,但是按照你已经拥有的东西进行编码,这样的东西应该可以工作:

在您的 Activity 中,定义一个成员变量来记住当前检查了哪些项目(注意,您必须保存/恢复该变量的值,以防 Activity 被破坏/重新创建,但这超出了此处的范围) :

Set<Long> mCheckedItemPositions = new HashSet<Long>();

像这样扩展您的OnCheckedChangeListener,以跟踪检查的位置:

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   if(isChecked) {
    holder.chkBox.setBackgroundResource(R.drawable.compselection_checked_btn);
    // you use the ID field to hold the position
    mCheckedItemPositions.add(buttonView.getId());             
   } else {
    holder.chkBox.setBackgroundResource(R.drawable.compselection_btn);           
    mCheckedItemPositions.remove(buttonView.getId()); 
   }

}

最后将其添加到 getView 方法的末尾、return 语句之前,以便根据 mCheckedPositions< 以正确的状态呈现复选框:

if (mCheckedPositions.contains(holder.chkBox.getId()) {
    holder.chkBox.setBackgroundResource(R.drawable.compselection_checked_btn);
} else {
    holder.chkBox.setBackgroundResource(R.drawable.compselection_btn);           
}

关于Android Gridview 中的复选框回收 View 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20776734/

相关文章:

Android TextToSpeech 不适用于 android 1.5

android - TextView 在 ListView 中的 ScrollView 内不可点击?

android - Google Play 商店中不支持的设备 - Flutter

android - phonegap 1.7 notificationCallback(此函数 this.sendJavascript(js))不起作用

Android 子类的数据绑定(bind)

android - 如果动态加载,SVG 不会在 android 和 safari 上播放动画

java - 滚动 recyclerview 时滞后

android - Jetpack Compose 全屏对话框

java - IllegalStateException:WorkManager 已经初始化

android - 在 Android 流中使用 flush() close() 的好处?