国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > Recycleview checkbox 复用出现混乱解决方法

Recycleview checkbox 复用出现混乱解决方法

来源:程序员人生   发布时间:2015-04-08 08:16:37 阅读次数:9347次

今天在做项目的1个我的收藏页面时要实现这么1个效果:
这里写图片描述
每一个item 前面都有1个checkbox ,主要布局是recycleview 。当我们选中某项时,继续往下滑动下面某个的checkbox也会变成选中状态。
解决方法1比较暴力 viewHolder.setIsRecyclable(false);
这样viewholder就不进行复用,也不存在checkbox 混乱选中的问题了,但是 你滑动的时候卡的要死。

第2种就是在获得到的bean中设置1个标志位,但是需要修改bean 。虽然只是加个字段的问题,但是不建议。

第3种就是用个辅助list记录选中的item. 这还不够,还要配合viewholder的settag().

1.在绑定view的时候
viewHolder.checkBox.setTag(new Integer(position));//设置tag 否则划回来时选中消失

    if (checkPositionlist != null) {
        ((MyViewHolder) holder).checkBox.setChecked((checkPositionlist.contains(new Integer(position)) ? true : false));
    } else {
        ((MyViewHolder) holder).checkBox.setChecked(false);
    }

2.选中监听事件:

viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { MyCollectionBaseBean baseBean = data.getFavoritesList().get(position); if (isChecked) { //Log.e("fangchao 1", viewHolder.checkBox.getTag() + ""); if (!checkPositionlist.contains(viewHolder.checkBox.getTag())) {//这句要有 ,否则不复用了,但是你再滑动回去的时候,都成了为选中。这是由于你的item滑出可视范围时,就会触发oncheckchange事件,所以用第1步绑定的tag进行2次判断。避免选中的丢失 checkeddata.add(baseBean); checkPositionlist.add(new Integer(position)); } } else { //Log.e("fangchao 2", viewHolder.checkBox.getTag() + ""); if (checkPositionlist.contains(viewHolder.checkBox.getTag()))//这句同上,2次判断{ checkeddata.remove(baseBean); checkPositionlist.remove(new Integer(position)); } } } });

完全代码,自己摘着看:

/** * Created By Fangchao On 2015/3/5 */ public class CollectionAdapter extends FooterAdapter { private MyCollectionListBean data;// 元数据 private List<MyCollectionBaseBean> checkeddata = new ArrayList<>();// 选中的数据 private ImageLoader mImageLoader; private List<Integer> checkPositionlist; public CollectionAdapter(MyCollectionListBean data) { this.data = data; mImageLoader = new ImageLoader(MyApplication.getInstance() .getRequestQueue(), BitmapCache.getInstance()); checkPositionlist = new ArrayList<>(); } public class MyViewHolder extends RecyclerView.ViewHolder { public TextView txt_onsale, txt_mj, txt_present, goodsname, goods_describe; public TextView price, mark_price, commentsnums; CheckBox checkBox; NetworkImageView goods_img; public View v; public MyViewHolder(View v) { super(v); this.v = v; txt_onsale = (TextView) v.findViewById(R.id.txt_onsale); txt_mj = (TextView) v.findViewById(R.id.txt_mj); txt_present = (TextView) v.findViewById(R.id.txt_present); goods_describe = (TextView) v.findViewById(R.id.tv_production_describ); goodsname = (TextView) v.findViewById(R.id.tv_goods_name); price = (TextView) v.findViewById(R.id.tv_goods_price); mark_price = (TextView) v.findViewById(R.id.tv_goods_markprice); commentsnums = (TextView) v.findViewById(R.id.tv_goods_commentsnums); checkBox = (CheckBox) v.findViewById(R.id.collection_cb); goods_img = (NetworkImageView) v.findViewById(R.id.mycollection_item_image); } } @Override public boolean useFooter() { return true; } @Override public RecyclerView.ViewHolder onCreateContentItemViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_collection, parent, false); MyViewHolder vh = new MyViewHolder(v); return vh; } @Override public void onBindContentItemView(RecyclerView.ViewHolder holder, final int position) { MyViewHolder viewHolder = (MyViewHolder) holder; // viewHolder.setIsRecyclable(false); viewHolder.goodsname.setText(data.getFavoritesList().get(position).getGoods().getGoodsName()); if (StringUtils.isBlank(data.getFavoritesList().get(position).getGoods().getGoodsSlogan())) { viewHolder.goods_describe.setVisibility(View.GONE); } else { viewHolder.goods_describe.setVisibility(View.VISIBLE); viewHolder.goods_describe.setText(data.getFavoritesList().get(position).getGoods().getGoodsSlogan()); } viewHolder.price.setText(PriceTools.formatStr(data.getFavoritesList().get(position).getGoods().getShopPrice())); viewHolder.price.setTextColor(Color.parseColor("#E5004B")); viewHolder.mark_price.setText(PriceTools.formatStr(data.getFavoritesList().get(position).getGoods().getMarketPrice())); viewHolder.mark_price.setBackgroundResource(0); viewHolder.mark_price.getPaint().setFlags( Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰 viewHolder.commentsnums.setText(data.getFavoritesList().get(position).getGoods().getCommentCount()); viewHolder.checkBox.setTag(new Integer(position));//设置tag 否则划回来时选中消失 //checkbox 复用问题 if (checkPositionlist != null) { ((MyViewHolder) holder).checkBox.setChecked((checkPositionlist.contains(new Integer(position)) ? true : false)); } else { ((MyViewHolder) holder).checkBox.setChecked(false); } //Log.e("ffff",position+""); viewHolder.goods_img.setDefaultImageResId(R.drawable.item_default_img); viewHolder.goods_img.setErrorImageResId(R.drawable.item_default_img_err); viewHolder.goods_img.setImageUrl(data.getFavoritesList().get(position).getGoods().getImgFile(), mImageLoader); if (data.getFavoritesList().get(position).getGoods().getIsOnSale().equals("false")) { //下架 viewHolder.price.setText("本商品已下架"); viewHolder.price.setTextColor(Color.GRAY); viewHolder.mark_price.setText(""); } else if (data.getFavoritesList().get(position).getGoods().isIsNoStock()) { //缺货 viewHolder.mark_price.setBackgroundResource(R.drawable.ticket_is_outof_date_bg); viewHolder.mark_price.setText(" 缺货 "); viewHolder.price.setTextColor(Color.GRAY); viewHolder.mark_price.getPaint().setFlags(0); } //cb 的选中事件 onchecked(viewHolder, position); } private void onchecked(final MyViewHolder viewHolder, final int position) { viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { MyCollectionBaseBean baseBean = data.getFavoritesList().get(position); if (isChecked) { //Log.e("fcccc 1", viewHolder.checkBox.getTag() + ""); if (!checkPositionlist.contains(viewHolder.checkBox.getTag())) { checkeddata.add(baseBean); checkPositionlist.add(new Integer(position)); } } else { //Log.e("fcccc 2", viewHolder.checkBox.getTag() + ""); if (checkPositionlist.contains(viewHolder.checkBox.getTag())) { checkeddata.remove(baseBean); checkPositionlist.remove(new Integer(position)); } } } }); } @Override public int getContentItemCount() { if (data != null) return data.getFavoritesList().size(); else return 0; } @Override public int getContentItemType(int position) { return 0; } public void getdata(MycheckListener listener) { listener.getcheckeddata(checkeddata); } public interface MycheckListener { void getcheckeddata(List<MyCollectionBaseBean> listBean); } }
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生