如何在RecyclerView和AsyncTask中禁用按钮 [英] How to disable button in RecyclerView and from AsyncTask

查看:91
本文介绍了如何在RecyclerView和AsyncTask中禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在翻新响应中点击RecyclerView后,我试图禁用按钮.

I am trying to disable button after click in RecyclerView from Retrofit response.

应用程序正在使用RecyclerView填充列表,而我正在使用Retrofit与后端REST API通信.一个项目内有两个按钮,单击即激活了改造客户端.并且如果来自API的响应成功,则应该禁用按钮.我遇到了两个问题: 前几项效果很好,但是在我从未单击过的滚动按钮几下后,它们也被禁用了; 其次是列表上的少量随机按钮仍然可以单击.

Application is using RecyclerView to populate list and I am using Retrofit to communicate to back-end REST API. There are two buttons inside one item and Retrofit client is activated on click. And if response from API is successful button should be disabled. I came across on two problems: first few items works just fine, but after few scrolls buttons I have never clicked on are disabled also; second was that little few random buttons further on in list are still clickable.

    public void onBindViewHolder(final NewsViewHolder holder, int position) {
        holder.btnPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//              init Retrofit Client
                JSONPlaceHolderAPI mAPIService;
                mAPIService = ApiUtils.getAPIServiceFetch();
                mAPIService.getNews(url).enqueue(new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> 
                                           response) {
                            holder.btnPositive.setVisibility(View.GONE);
                        }
                    }
                    @Override
                    public void onFailure(Call<Result> call, Throwable t) {
//                      do code
                    }
                });

            }
        });
        holder.btnNegative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONPlaceHolderAPI mAPIService;
                mAPIService = ApiUtils.getAPIServiceFetch();
                mAPIService.getNews(url).enqueue(new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> 
                                           response) {
                            holder.btnNegative.setEnabled(false);
                        }
                    }
                    @Override
                    public void onFailure(Call<Result> call, Throwable t) {
                    // do code
                    }
                });

            }
        });
    }

我想问题出在Retrofit中的某个地方,它使用后台线程或AsyncTask.

I suppose problem is somewhere in Retrofit where it uses background threads or AsyncTask.

推荐答案

这种情况发生在回收者视图回收(顾名思义)视图时,该视图对您可见,并且为每个项目集提供相同的ID集.导致此问题.

This happens as the recycler view recycles(as the name suggests) the view that is visible to you and gives the same set of ids to every set of items which causes this problem .

此问题有2个解决方案:

This problem has 2 solution :

1)停止recyclerview的回收行为,如下所示:-

1) Stop the recycle behaviour of recyclerview as follows:-

     @Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
    myViewHolder.setIsRecyclable(false);
}

但是我强烈拒绝使用此原因,因为回收是我们使用recyclerview的原因.

But i strongly deny to use this cause recycling is reason why we use recyclerview.

2)使用POJO类:-

2) Using a POJO Class:-

最好的解决方案是使用POJO类,该类将具有两个变量,第一个是value,第二个是布尔变量,表明是否禁用了该项目.对于您要禁用的项目,将POJO布尔变量的值设置为true,并且在onBindViewHolder方法中,仅禁用在布尔变量中设置为false的项目的按钮.

The best solution for it is to use a POJO class which will have two variables the first one is value and the second one is a boolean variable which show is the item disabled or not . Set the value of POJO boolean variable to true for the items u want to disable and in onBindViewHolder method only disable the buttons for the items which has false set in boolean variable .

如果您仍然感到困惑,请与我联系,我会给您发送一个示例.

If you still are confused contact me i will send you an example of it .

这篇关于如何在RecyclerView和AsyncTask中禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆