简体   繁体   中英

RecyclerView ItemDecoration and Iterator.remove() problems

I have a recyclerView with an item decoration like this:

public class VerticalItemDecoration extends RecyclerView.ItemDecoration{
    private int space;

    public VerticalItemDecoration(int space){
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state){
        outRect.top = space;

        Log.e("Decor", "Parent: " + parent + " Childs: " + parent.getChildCount());

        if (parent.getChildLayoutPosition(view) == 0){
            outRect.top = 0;
        }
    }
}

Every 10 seconds, i am iterating through the data in this recycler view and calling iterator.remove() . Right after that, i am notifying the adapter that the item was removed. However after removing the item, getItemOffsets() is called twice for that recycler view. The first time it shows 1 child view, which is correct because i removed one. But right after that it gets called again and reports 2 child views. This is only happening when removing items. What is causing this?

I am iterating like this:

for (Iterator<Client> iterator = AdapterClients.listClients.iterator(); iterator.hasNext();){
                                    Client client1 = iterator.next();

                                    if (client.getMacAddress().equals(client1.getMacAddress())){
                                        //This client is already on the adapter
                                        clientExists = true;

                                        //Make sure the client is still reachable
                                        if (ApManager.isClientReachable(client1, 3000)){
                                            //Client is still reachable
                                            //Update the client's information
                                            if (client1.getHostName().equals("Unknown Host Name")){
                                                client1.setHostName(ApManager.getHostName(client1));
                                                notifyAdapter("itemChanged", AdapterClients.listClients.indexOf(client1));
                                            }
                                        }else{
                                            //Client is no longer reachable, remove it from the adapter
                                            int index = AdapterClients.listClients.indexOf(client1);
                                            iterator.remove();
                                            Log.e("Arp", "index: " + index + " objIndex: " + AdapterClients.listClients.indexOf(client1));
                                            notifyAdapter("itemRemoved", index);
                                            DataLogger.log("Client " + client1.getIpAddress() + " disconnected from \"" + ApManager.getActiveHotspot().getSsid() + "\"");
                                        }
                                    }
                                }

The notifyAdapter() method looks like this:

private static void notifyAdapter(final String action, final int index){
    new Handler(Looper.getMainLooper()).post(new Runnable(){
        @Override
        public void run(){
            switch (action){
                case "itemInserted":
                    Log.e("Arp", "itemInserted");
                    FragmentClients.adapterClients.notifyItemInserted(index);
                    break;

                case "itemRemoved":
                    Log.e("Arp", "itemRemoved");
                    FragmentClients.adapterClients.notifyItemRemoved(index);
                    Log.e("Arp", "after itemRemoved");
                    break;

                //Other cases...
            }

            FragmentClients.updateText();

            //FragmentClients.adapterClients.notifyItemChanged(0);

            FragmentHotspots.adapterHotspots.notifyItemChanged(AdapterHotspots.listHotspots.indexOf(ApManager.getActiveHotspot()));
        }
    });
}

This is the output from logcat:

05-23 18:58:57.143 4726-5658/com.example  E/Arp: index: 0 objIndex: -1
05-23 18:58:57.154 4726-4726/com.example  E/Arp: itemRemoved
05-23 18:58:57.154 4726-4726/com.example  E/Arp: after itemRemoved
05-23 18:58:57.171 4726-4726/com.example  E/Decor: Parent: android.support.v7.widget.RecyclerView{b0a533b VFED.... .F....I. 0,0-1080,1509 #7f0e009b app:id/recyclerViewClients} Childs: 1
05-23 18:58:57.174 4726-4726/com.example  E/Decor: Parent: android.support.v7.widget.RecyclerView{b0a533b VFED.... .F....I. 0,0-1080,1509 #7f0e009b app:id/recyclerViewClients} Childs: 2
05-23 18:58:57.187 4726-4726/com.example  E/Decor: Parent: android.support.v7.widget.RecyclerView{3d927634 VFED.... ......I. 0,0-1080,1509 #7f0e009d app:id/recyclerViewHotspots} Childs: 1

NOTE: Using notifyDataSetChanged() instead of notifyItemRemoved() works, but then the animation doesn't show.

I have fixed this by changing the getItemOffsets() method to this

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state){
    outRect.top = space;

    Log.e("Decor", "Parent: " + parent + " Childs: " + parent.getChildCount() + " pos: " + parent.getChildLayoutPosition(view));

    if (parent.getChildCount() == 1){
        outRect.top = 0;
    }
}

It seems like the views are updating in the wrong order when calling notifyItemRemoved() because during initialization the log is:

05-23 20:51:56.889 14129-14129/com.example  E/Decor: Parent: android.support.v7.widget.RecyclerView{323b3d54 VFED.... ......ID 0,0-1080,1845 #7f0e009d app:id/recyclerViewHotspots} Childs: 1 pos: 0
05-23 20:51:56.895 14129-14129/com.example  E/Decor: Parent: android.support.v7.widget.RecyclerView{323b3d54 VFED.... ......ID 0,0-1080,1845 #7f0e009d app:id/recyclerViewHotspots} Childs: 2 pos: 1
05-23 20:51:56.899 14129-14129/com.example  E/Decor: Parent: android.support.v7.widget.RecyclerView{323b3d54 VFED.... ......ID 0,0-1080,1845 #7f0e009d app:id/recyclerViewHotspots} Childs: 3 pos: 2

and after deleting the first item the log is:

05-23 20:52:15.498 14129-14129/com.example  E/Decor: Parent: android.support.v7.widget.RecyclerView{323b3d54 VFED.... .F....I. 0,0-1080,1509 #7f0e009d app:id/recyclerViewHotspots} Childs: 1 pos: 1
05-23 20:52:15.499 14129-14129/com.example  E/Decor: Parent: android.support.v7.widget.RecyclerView{323b3d54 VFED.... .F....ID 0,0-1080,1509 #7f0e009d app:id/recyclerViewHotspots} Childs: 2 pos: 2
05-23 20:52:15.499 14129-14129/com.example  E/Decor: Parent: android.support.v7.widget.RecyclerView{323b3d54 VFED.... .F....ID 0,0-1080,1509 #7f0e009d app:id/recyclerViewHotspots} Childs: 3 pos: 0

So clearly after calling notifyItemRemoved , it doesn't properly remove the view from the recycler view and it updates it last instead of first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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