RecyclerView ItemDecoration和Iterator.remove()问题

时间:2016-05-24 02:59:47

标签: java android iterator android-recyclerview

我有一个带有像这样的项目装饰的recyclerView:

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;
        }
    }
}

每10秒钟,我在此回收站视图中迭代数据并调用iterator.remove()。在那之后,我通知适配器项目已被删除。但是,在删除项目后,将为该回收站视图调用两次getItemOffsets()。它第一次显示1个子视图,这是正确的,因为我删除了一个。但在此之后它再次被调用并报告2个子视图。只有在删除项目时才会发生这种情况。造成这种情况的原因是什么?

我正在这样迭代:

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() + "\"");
                                        }
                                    }
                                }

notifyAdapter()方法如下所示:

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()));
        }
    });
}

这是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

注意:使用notifyDataSetChanged()代替notifyItemRemoved()有效,但动画未显示。

1 个答案:

答案 0 :(得分:0)

我已通过将getItemOffsets()方法更改为此

来解决此问题
@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;
    }
}

在调用notifyItemRemoved()时,似乎视图以错误的顺序更新,因为在初始化期间,日志是:

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

删除第一项后,日志为:

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

在调用notifyItemRemoved之后很清楚,它没有从回收站视图中正确删除视图,而是最后更新它而不是第一次。