亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將項目添加到收藏夾數據庫時更改圖標顏色

將項目添加到收藏夾數據庫時更改圖標顏色

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 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號