第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將項(xiàng)目添加到收藏夾數(shù)據(jù)庫(kù)時(shí)更改圖標(biāo)顏色

將項(xiàng)目添加到收藏夾數(shù)據(jù)庫(kù)時(shí)更改圖標(biāo)顏色

largeQ 2023-04-26 17:19:17
當(dāng)我單擊心形圖標(biāo)時(shí),它會(huì)將項(xiàng)目發(fā)送到收藏夾并更改顏色,但是當(dāng)加載活動(dòng)時(shí),圖標(biāo)會(huì)恢復(fù)正常顏色,但該項(xiàng)目仍在收藏夾中。我如何檢查收藏夾中的項(xiàng)目是否基于數(shù)據(jù)庫(kù)房間的監(jiān)聽(tīng)器之類的東西更改圖標(biāo)顏色?這是適配器:-    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貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

我可以在上面的代碼中看到 2 個(gè)問(wèn)題,

  1. 當(dāng)我們添加和刪除時(shí)您正在更新數(shù)據(jù)庫(kù),這很好,但是您處理本地視圖引用的方式是錯(cuò)誤的。

    原因:因?yàn)樵谀愕那闆r下,不僅當(dāng)你轉(zhuǎn)到另一個(gè)屏幕時(shí)它不會(huì)工作,如果你滾動(dòng)更多如果你有更多的項(xiàng)目然后回來(lái)它也不會(huì)工作,因?yàn)榛厥找晥D重用你更新的視圖在檢查監(jiān)聽(tīng)器中,這導(dǎo)致了第二個(gè)問(wèn)題

  2. 在 onBindData 中,你總是應(yīng)該使用非收藏圖標(biāo),所以每當(dāng)你滾動(dòng)和查看重用它時(shí),它只會(huì)顯示非收藏圖標(biāo),你應(yīng)該檢查該項(xiàng)目是否是收藏,你應(yīng)該更新視圖

例如,你應(yīng)該像下面那樣

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

    }

}

請(qǐng)根據(jù)您的項(xiàng)目修改代碼。


查看完整回答
反對(duì) 回復(fù) 2023-04-26
  • 1 回答
  • 0 關(guān)注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)