編輯:關於Android編程
下面的代碼中報錯了,代碼如下
@Override p public View getView(final int position, final View convertView, final ViewGroup parent) { final ViewHolder viewHolder; View view = convertView; if (view == null) { view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false); viewHolder = new ViewHolder(); viewHolder.textTitle = (TextView) view.findViewById(R.id.title); viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox); viewHolder.checkBox.setTag(position); view.setTag(viewHolder); viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview); } else { viewHolder = (ViewHolder) view.getTag(); viewHolder.checkBox.getTag(position); } viewHolder.textTitle.setText(getItem(position).getTitle()); viewHolder.checkBox.setChecked(myIntegerArrayList.contains(position)); viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { myIntegerArrayList.add(position); } else { myIntegerArrayList.remove((Object) position); } } }); return view; } } private static class ViewHolder { TextView textTitle; CheckBox checkBox; } }
該復選框本身工作正常。位置正確添加到我的ArrayList,但如果(假設我選中的項目在位置1)我把listview滾動到底部,然後再滾回去,選中取消了....
刪掉代碼viewHolder.checkBox.setTag(position);
把 viewHolder.checkBox.getTag();放到 onCheckedChanged 中,你按照下面的代碼改動
@Override public View getView(final int position, final View convertView, final ViewGroup parent) { final ViewHolder viewHolder; View view = convertView; if (view == null) { view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false); viewHolder = new ViewHolder(); viewHolder.textTitle = (TextView) view.findViewById(R.id.title); viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox); view.setTag(viewHolder); viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview); } else { viewHolder = (ViewHolder) view.getTag(); viewHolder.checkBox.getTag(position); } viewHolder.checkBox.setTag(position); viewHolder.textTitle.setText(getItem(position).getTitle()); viewHolder.checkBox.setChecked(myIntegerArrayList.contains(position)); viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { myIntegerArrayList.add(viewHolder.checkBox.getTag()); } else { myIntegerArrayList.remove((Object) viewHolder.checkBox.getTag()); } } }); return view; } }
讓你刪掉 viewHolder.checkBox.setTag(position); 是為了 回收ListView,你需要報錯位置在tag中,並讓 onCheckedChanged 獲得到
添加viewHolder.checkBox.getTag() 也是同樣的原因
前言:關於使用ViewPageIndicator和ViewPager實現Tab導航,在開發社區裡已經有一堆的博客對其進行了介紹,如果我還在這裡寫如何去實現,那簡直就是老生
概述本篇主要分析的是touch事件的分發機制,網上關於這個知識點的分析文章非常多。但是還是想通過結合自身的總結,來加深自己的理解。對於事件分發機制,我將使用兩篇文章對其進
1.介紹Runtime Permissions官方說明Android 6.0之前,權限在應用安裝過程中只詢問一次,以列表的形式展現給用戶,然而大多數用戶並不會注意到這些,
本文實例講述了Android編程根據系列圖片繪制動畫的方法。分享給大家供大家參考,具體如下:一、采用系統提供的Animation類,用自帶的方法其中的animation.