单击 RecyclerView 项目后禁用按钮时出现问题

单击后,我想禁用 RecyclerView 中某些项目的两个按钮。

我在单击后更新的 POJO 类中添加了布尔值来保持按钮 (isClicked) 的状态。

@Override

    public void onBindViewHolder(final NewsViewHolder holder, final int position) {


        if(Fragment.List.get(holder.getAdapterPosition()).isTrueBtnClicked){

            holder.btnPositive.setEnabled(false);

            holder.btnPositive.setBackgroundResource(R.color.darkGray);

            holder.btnNegative.setEnabled(false);

            holder.btnNegative.setBackgroundResource(R.color.darkGray);


        }else{

            //to do

        }

        //used position instead getAdapterPosition()

        if(BlicFragment.List.get(position).isLieBtnClicked){


            holder.btnPositive.setEnabled(false);

            holder.btnPositive.setBackgroundResource(R.color.darkGray);

            holder.btnNegative.setEnabled(false);

            holder.btnNegative.setBackgroundResource(R.color.darkGray);


        }else{

            //holder.btnPositive.setEnabled(false);

        }



        holder.btnPositive.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                //SendPost postingToDb = new SendPost();

                //postingToDb.sendPost(sTitle, sDesc, url, sDate, sImgUrl[0], 1, 0, "Blic");

                JSONPlaceHolderAPI mAPIService;

                mAPIService = ApiUtils.getAPIServiceFetch();

                mAPIService.getNews(url).enqueue(new Callback<Result>() {

                    @Override

                    public void onResponse(Call<Result> call, Response<Result> response) {

                        if(response.body().getSuccess()==1){

                            isPositiveBtnClicked(holder);

                        }else{

                            isPositiveBtnClicked(holder);

                        }

                    }

我正面临那个按钮被禁用,接下来的五个按钮不受影响。但是被点击按钮的每六个按钮都会被禁用。最终所有其他人都被禁用。


牧羊人nacy
浏览 99回答 1
1回答

天涯尽头无女友

是的,这正在发生,因为 Recylerview 总是重复使用或渲染从屏幕出来的屏幕视图。因此,请确保您应该完全将视图恢复正常,就像在某些情况下视图正在更改以使用某些条件启用状态那么必须if()有else使它们禁用的部分。例如在你的情况下......&nbsp; &nbsp; &nbsp; &nbsp; if(Fragment.List.get(holder.getAdapterPosition()).isTrueBtnClicked){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.btnPositive.setEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.btnPositive.setBackgroundResource(R.color.darkGray);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.btnNegative.setEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.btnNegative.setBackgroundResource(R.color.darkGray);&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.btnPositive.setEnabled(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.btnPositive.setBackgroundResource(R.color.greenEnable);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.btnNegative.setEnabled(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holder.btnNegative.setBackgroundResource(R.color.greenEnable);&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java