简体   繁体   English

RecyclerView中的更新项目

[英]Update item in RecyclerView

I have an api that returns me items per page, in this case I always ask it to send me 12 items if the items received are equal to 12 the page is increased, if not kept. 我有一个api,可让我每页返回项目,在这种情况下,如果收到的项目等于12,则我总是要求它向我发送12个项目,如果页面不保存,页面会增加。 I have the following example where my api I sent the page 1 but in this case it returns 4 items which I show in my RecyclerView, after pressing my button to get more data returns me 6 items ie the 4 anterios and 2 more. 我有以下示例,我的api发送了第1页,但在这种情况下,它返回了4个项目,这些项目显示在RecyclerView中,按我的按钮获取更多数据后,我返回了6个项目,即4个anterios和2个以上。 What I want to do is compare the data that the api returns with those of the RecyclerView and only add the different items to it. 我想做的是将api返回的数据与RecyclerView的数据进行比较,然后仅向其中添加不同的项目。 Example: 例:

first call data -> A B C D are added to the RecyclerView
second call data -> A B C D E F
compare data from call 1 with call 2 and add E F because they are different

the final result to be shown in the RecyclerView would be the following A B C D E F

this is my code: 这是我的代码:

mData.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mData.size());
notifyDataSetChanged();
//this lines deletes the final button, and after add the new data 
JSONArray res = response_json.getJSONArray("res");
for(int i=0;i<mData.size();i++){
    if(i >= row_index){ //row_index I identify the index of the page to update from it
        for(int j=0;j<res.length();j++){
            JSONObject item = res.getJSONObject(j);
            if(mData.get(i).getId() != item.getInt("id")){
                //add diferent data
                obj = new Obj();
                obj.setId_pedido(item.getInt("id"));
                obj.setValor(item.getString("valor"));
                mData.add(obj);
                notifyDataSetChanged();
            }
        }
    }
}
Obj mas = new Obj();
mas.setId_pedido(0);
mas.setValor("OBtener mas data");
mData.add(mas);
//Add the final button

All that I do in my adapter because the button I always add at the end as the last item. 我在适配器中所做的所有操作,因为我总是在最后添加按钮作为最后一项。 When running it does not work, my app freeze. 运行时不起作用,我的应用程序冻结。 How can I solve that? 我该如何解决?

Thank you very much for your time and assistance on this matter. 非常感谢您的时间和协助。

The problem is because you always refresh the RecyclerView adapter when you don't need it. 问题是因为不需要时总是刷新RecyclerView适配器。

This code isn't good: 这段代码不好:

mData.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mData.size());
notifyDataSetChanged();

because you're telling the adapter to refresh itself 3 times. 因为您要告诉适配器刷新3次。 You only need call the following code whenever you remove an item: 只要删除项目,就只需要调用以下代码:

mData.remove(position);
notifyItemRemoved(position);

You're also telling the adapter to update all the items whenever you add an item: 您还告诉适配器在添加项目时更新所有项目:

JSONArray res = response_json.getJSONArray("res");
for(int i=0;i<mData.size();i++){
    ...
        for(int j=0;j<res.length();j++){
            JSONObject item = res.getJSONObject(j);
            if(mData.get(i).getId() != item.getInt("id")){
                //add diferent data
                ...
                mData.add(obj);
                notifyDataSetChanged();
            }
        }
    ...
}

which simply means you're telling the adapter to refresh all the items x times. 这只是意味着您要告诉适配器刷新所有项目x次。 Where x is your total new items. 其中x是您的新项目总数。 This will freezing your UI for a while. 这将冻结您的UI一段时间。

So, you need to update the adapter only when you've finished adding the items. 因此,仅在完成添加项后才需要更新适配器。 Something like this: 像这样:

JSONArray res = response_json.getJSONArray("res");
for(int i=0;i<mData.size();i++){
    ...
        for(int j=0;j<res.length();j++){
            ...
            if(mData.get(i).getId() != item.getInt("id")){
                ...
                mData.add(obj);
            }
        }
    ...
}

notifyDataSetChanged();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM