public VIEw getVIEw(final int position,VIEw convertVIEw,VIEwGroup parent) { // Todo auto-generated method stub final VIEwHolder holder;final boolean[] itemChecked=new boolean[30]; LayoutInflater inflater = context.getLayoutInflater(); if(convertVIEw==null) { convertVIEw = inflater.inflate(R.layout.custom_List,null); holder = new VIEwHolder(); holder.txtVIEwTitle = (TextVIEw) convertVIEw.findVIEwByID(R.ID.Title_text); holder.txtVIEwDescription = (TextVIEw) convertVIEw.findVIEwByID(R.ID.description_text); holder.cb=(CheckBox) convertVIEw.findVIEwByID(R.ID.cb); convertVIEw.setTag(holder); } else { holder=(VIEwHolder)convertVIEw.getTag(); } holder.cb.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() { @OverrIDe public voID onCheckedChanged(Compoundbutton buttonVIEw,boolean isChecked) { // Todo auto-generated method stub itemChecked[position] = isChecked; if(itemChecked[position]) { holder.cb.setChecked(true); } else { holder.cb.setChecked(false); } holder.txtVIEwTitle.setText(Title[position]); holder.txtVIEwDescription.setText(description[position]); holder.cb.setChecked(itemChecked[position]); holder.txtVIEwDescription.setFocusable(false); holder.txtVIEwTitle.setFocusable(false);return convertVIEw; } }解决方法 每当需要绘制以前不可见的列表项时调用getVIEw().由于每次调用此方法时都会重新创建itemChecked [],因此您将不会选中新复选框,并为每个生成的视图选择不同的数组. (Java中的final不会像C那样使该领域独一无二)
最简单的解决方法是让itemChecked成为一个类成员,并根据该选项设置/恢复复选框状态.
public class Mylistadapter extends ArrayAdapter<Object> { private final boolean[] mCheckedState; private final Context mContext; public Mylistadapter(Context context,int resource,int textVIEwResourceID,List<Object> objects) { super(context,resource,textVIEwResourceID,objects); mCheckedState = new boolean[objects.size()]; mContext = context; } @OverrIDe public VIEw getVIEw(int position,VIEwGroup parent) { // simplifIEd to just a CheckBox // VIEwHolder and OnCheckedchangelistener stuff left out CheckBox result = (CheckBox)convertVIEw; if (result == null) { result = new CheckBox(mContext); } result.setChecked(mCheckedState[position]); return result; }}总结
以上是内存溢出为你收集整理的android – 在自定义列表视图中滚动检查复选框全部内容,希望文章能够帮你解决android – 在自定义列表视图中滚动检查复选框所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)