RecyclerView的局部刷新Item 单选 隐藏控件 整体布局闪烁问题

情景:公司项目要求展示卡片,单选后展示详情介绍。

1、显示列表后,单选调用 

adapter.notifyItemChanged(selected);
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        VipCardBuyHolder helper = (VipCardBuyHolder) holder;
        VipTypeBean item = companyList.get(position);
        String url = (String) helper.tv_card_img.getTag();
        if (item.getPhoto() != null) {
            if (TextUtils.isEmpty(url) || !url.equals(item.getPhoto())) {
                Glide.with(activity).load(item.getPhoto()).asBitmap().into(helper.tv_card_img);
                helper.tv_card_img.setTag(url);
            }
        }
        if (item.getStatus() != null && item.getStatus().equals("1")) {//可选择背景状态
            helper.iv_to_selected.setBackground(activity.getResources().getDrawable(R.drawable.selector_card_state));
        } else {//禁止选择背景
            helper.iv_to_selected.setBackground(activity.getResources().getDrawable(R.drawable.img_sign_not_select));
        }
        helper.tv_vip_card_des.setText( TextUtils.isEmpty(item.getCardDesc()) ? "" : item.getCardDesc());
        helper.tv_vip_card_des.setVisibility(selected == helper.getAdapterPosition() ? View.VISIBLE : View.GONE);
        helper.iv_to_selected.setSelected(selected == helper.getAdapterPosition());

        holder.itemView.setTag(position);
    }

整体布局出现闪屏,百度看过好多有关相似问题,都说是默认动画引起的。
recyclerView.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
((SimpleItemAnimator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);//要设置为false,就可以不显示动画了,也就解决了闪烁问题
但是对于当前情景没有效果。

本文参考:https://blog.csdn.net/L0123456L/article/details/86180307?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

直接上代码:

 private Activity activity;
    private final String QUIT = "quit";
    private final String SELECTED = "selected";
    private List<VipTypeBean> companyList;
    private int selected = -1;
    private int quit;

    public void setSelected(int enter) {
        this.quit = selected;
        notifyItemChanged(quit,QUIT);
        this.selected = enter;
        notifyItemChanged(selected,SELECTED);
    }
    public VipCardToBuyAdapter(Activity activity, List<VipTypeBean> companyList) {
        this.activity = activity;
        this.companyList = companyList;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        VipCardBuyHolder helper = (VipCardBuyHolder) holder;
        VipTypeBean item = companyList.get(position);
        String url = (String) helper.tv_card_img.getTag();
        if (item.getPhoto() != null) {
            if (TextUtils.isEmpty(url) || !url.equals(item.getPhoto())) {
                Glide.with(activity).load(item.getPhoto()).asBitmap().into(helper.tv_card_img);
                helper.tv_card_img.setTag(url);
            }
        }
        if (item.getStatus() != null && item.getStatus().equals("1")) {
            helper.iv_to_selected.setBackground(activity.getResources().getDrawable(R.drawable.selector_card_state));
        } else {
            helper.iv_to_selected.setBackground(activity.getResources().getDrawable(R.drawable.img_sign_not_select));
        }
        helper.tv_vip_card_des.setText( TextUtils.isEmpty(item.getCardDesc()) ? "" : item.getCardDesc());
        helper.tv_vip_card_des.setVisibility(selected == helper.getAdapterPosition() ? View.VISIBLE : View.GONE);
        helper.iv_to_selected.setSelected(selected == helper.getAdapterPosition());

        holder.itemView.setTag(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        }
        else {
            VipCardBuyHolder helper = (VipCardBuyHolder) holder;
            for (Object payload : payloads) {
                switch (String.valueOf(payload)) {
                    case QUIT:
                        if (quit == helper.getPosition()) {
                            helper.iv_to_selected.setSelected(false);
                            helper.tv_vip_card_des.setVisibility(View.GONE);
                        }

                        break;
                    case SELECTED:
                        if (selected == helper.getPosition()) {
                            helper.iv_to_selected.setSelected(true);
                            helper.tv_vip_card_des.setVisibility(View.VISIBLE);
                        }
                        break;
                }
            }
        }

    }

如果其他朋友有更好的解决方案,请留言告知,谢谢!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值