【问题标题】:Stop recyclerview from trying to update when "Favourite" button is clicked单击“收藏夹”按钮时停止recyclerview尝试更新
【发布时间】:2020-07-07 21:07:46
【问题描述】:

我的 recyclerview 的每一行都有一个“收藏夹”按钮,用户在喜欢图像时单击该按钮(显然)。每一行都是一个卡片视图,我只想在用户打开片段时“翻转”。

当用户单击按钮时,我用“Y”或“N”更新我的数据库。 我的问题是即使列表没有更改,我的 recyclerview 也会刷新。当它刷新我所有的卡片时,我不想要的翻转。单击按钮时如何阻止recyclerview更新?

这是我的适配器类

 @Override
    public void onBindViewHolder(@NotNull final ClothesViewHolder holder, final int position) {


        String image;

        if (flip) {
                holder.flipView.flipTheView();
            }
            ClothingItem current = mClothingItems.get(position);
            holder.itemNameView.setText(current.getItem());
            holder.categoryNameView.setText(current.getCategory());
            holder.seasonNameView.setText(current.getSeason());

            Integer yesCount = current.getYesCount();
            Integer noCount = current.getNoCount();


            if (current.getFavourite().equalsIgnoreCase("N")) {
                holder.animationView.setProgress(0);
            }
            else {
                holder.animationView.setProgress(1);
            }

            holder.yesTextView.setText(String.valueOf(yesCount));
            holder.noTextView.setText(String.valueOf(noCount));
            image = current.getPhotoPath();
            Glide.with(holder.cardView)
                    .load(image)
                    .into(holder.pictureView);

            flip = false;

        } catch (NullPointerException e) {
            Log.e("Picture","onBindViweHolder: Null Point:" + e.getMessage());
        }

        holder.animationView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                listener.onFavouriteClick(position);


            }
        });

 public interface clickButtons {

        void onFavouriteClick(int position);

    }

片段类

 @Override
    public void onFavouriteClick(int position) {

        RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(position);
        LottieAnimationView animationView = holder.itemView.findViewById(R.id.favouriteAnimation);

        ClothingItem item = springList.get(position);
        final Long id = item.getId();

        if (animationView.getProgress() > 0) {

            animationView.setProgress(0);
            mClothingViewModel.updateFavourite(id.intValue(), "N");
            adapter.notifyItemChanged(position,"favourite");

       } else if (animationView.getProgress() == 0) {

            animationView.playAnimation();
         
            mClothingViewModel.updateFavourite(id.intValue(),"Y");

        }
    }

我尝试将 onBindViewHolder 与有效负载一起使用,但得到了相同的结果。我认为我没有正确调用它

adapter.notifyItemChanged(position,"favourite");

适配器类

    @Override
    public void onBindViewHolder(final ClothesViewHolder holder ,final int position, final List<Object> payloads){
        String image;

        if(!payloads.isEmpty()) {
            ClothingItem current = mClothingItems.get(position);
            holder.itemNameView.setText(current.getItem());
            holder.categoryNameView.setText(current.getCategory());
            holder.seasonNameView.setText(current.getSeason());

            Integer yesCount = current.getYesCount();
            Integer noCount = current.getNoCount();

            if(yesCount == null) {

                yesCount = 0;
            }
            if (noCount == null) {

                noCount = 0;
            }

            if (current.getFavourite() == null || current.getFavourite().equalsIgnoreCase("N")) {
                holder.animationView.setProgress(0);
            }
            else {
                holder.animationView.setProgress(1);
            }

//            Log.d("Counting","Yes count " + yesCount + " no count " + noCount);

            holder.yesTextView.setText(String.valueOf(yesCount));
            holder.noTextView.setText(String.valueOf(noCount));
            image = current.getPhotoPath();
            Glide.with(holder.cardView)
                    .load(image)
                    .into(holder.pictureView);

        } else {

            onBindViewHolder(holder,position);
        }
    }

片段

 @Override
    public void onFavouriteClick(int position) {

        RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(position);
        LottieAnimationView animationView = holder.itemView.findViewById(R.id.favouriteAnimation);

        ClothingItem item = springList.get(position);
        final Long id = item.getId();

        if (animationView.getProgress() > 0) {

            animationView.setProgress(0);
            mClothingViewModel.updateFavourite(id.intValue(), "N");
            adapter.notifyItemChanged(position,"favourite");

       } else if (animationView.getProgress() == 0) {

            animationView.playAnimation();

            mClothingViewModel.updateFavourite(id.intValue(),"Y");
              
            adapter.notifyItemChanged(position,"favourite");
        }
    }

【问题讨论】:

    标签: java android android-recyclerview android-room


    【解决方案1】:

    当调用 notifydataSetChanged 时,Recyclerview 会更新或刷新。 尝试删除 click 事件中的 notifyDataSetChanged 可能

    【讨论】:

    • 嗨 Rommy,我删除了它,但 recyclerview 不断刷新
    猜你喜欢
    • 2015-07-31
    • 2016-11-07
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2015-04-25
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多