为了账号安全,请及时绑定邮箱和手机立即绑定

将项目添加到收藏夹数据库时更改图标颜色

将项目添加到收藏夹数据库时更改图标颜色

largeQ 2023-04-26 17:19:17
当我单击心形图标时,它会将项目发送到收藏夹并更改颜色,但是当加载活动时,图标会恢复正常颜色,但该项目仍在收藏夹中。我如何检查收藏夹中的项目是否基于数据库房间的监听器之类的东西更改图标颜色?这是适配器:-    public class BSAdapter extends RecyclerView.Adapter<BSAdapter.BestSellerHolder> {    private Context context;    private List<ProductsBestSeller> bestSellerList;    public BSAdapter(Context context, List<ProductsBestSeller> bestSellerList) {        this.context = context;        this.bestSellerList = bestSellerList;    }    @Override    public BestSellerHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View bestSellerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.default_product_items_horizontal, parent, false);        return new BestSellerHolder(bestSellerView);    }    @Override    public void onBindViewHolder(BestSellerHolder holder, int position) {        ProductsBestSeller bestSeller = bestSellerList.get(position);        holder.onBindData(bestSeller);    }    @Override    public int getItemCount() {        if (bestSellerList != null) {            return bestSellerList.size();        } else {            return 0;        }    }    public class BestSellerHolder extends RecyclerView.ViewHolder {        TextView productName, productPrice;        ImageView productIcon;        CheckBox favouriteIcon;        public BestSellerHolder(View itemView) {            super(itemView);            productIcon = itemView.findViewById(R.id.product_icon_horizontal);            productName = itemView.findViewById(R.id.product_name_horizontal);            productPrice = itemView.findViewById(R.id.product_price_horizontal);            favouriteIcon = itemView.findViewById(R.id.favourite_icon_horizontal);        }
查看完整描述

1 回答

?
森栏

TA贡献1810条经验 获得超5个赞

我可以在上面的代码中看到 2 个问题,

  1. 当我们添加和删除时您正在更新数据库,这很好,但是您处理本地视图引用的方式是错误的。

    原因:因为在你的情况下,不仅当你转到另一个屏幕时它不会工作,如果你滚动更多如果你有更多的项目然后回来它也不会工作,因为回收视图重用你更新的视图在检查监听器中,这导致了第二个问题

  2. 在 onBindData 中,你总是应该使用非收藏图标,所以每当你滚动和查看重用它时,它只会显示非收藏图标,你应该检查该项目是否是收藏,你应该更新视图

例如,你应该像下面那样

override fun onBindViewHolder(holder: VM, position: Int) {

    val item = items.get(position)


    if (item.favourite == 0) {

        holder.name.text = item.name

    } else {

        holder.name.text = item.name + " Liked "

    }


    holder.favouriteIcon.setOnCheckedChangeListener { compoundButton, isChecked ->

        // Should not update local view reference here

        if(isChecked) {

            // Update the local reference object, Just not to update from DB

            item.favourite = 1

            // Do the logic to update the DB to add the item in Fav

        } else {

            // Update the local reference object, Just not to update from DB

            item.favourite = 0

            // Do the logic to update to remove the item from Fav list

        }

        notifyItemChanged(position) // Helps to update the particular item

    }

}

请根据您的项目修改代码。


查看完整回答
反对 回复 2023-04-26
  • 1 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信