Search code examples
androidfirebaseandroid-recyclerviewfirebaseui

Some Firebase UI Recylerview items showing wrong position after onItemRangeMoved event


I'm trying to setup a pop-up menu inside my firebase RecyclerView adapter. It works fine when the data is still and when new data is added. But, when an old item moves to the top, it messes up the positions.

  holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        switch (menuItem.getItemId()){
                            case R.id.remove:

                                break;
                        }
                        return false;
                    }
                });
                System.out.println(getRef(position).getKey());
                System.out.println(position);

                p.show();
                return false;
            }
        });

This function is inside OnBindViewHolder. It prints statements showing correct data when loading for the first time. But after an item moved, first 2-5 items' position gets duplicated.

Data model is completely fine. But something is wrong with the position variable.

Need Help. :(


Solution

  • You can't use the position variable because that's only valid in the current message queue loop. Since your click listener is going to happen way later, you'll want to use getAdapterPosition() instead.