0

I have RecyclerView which is HORIZONTAL. Width of each item in it nearly equals the total width of recycler. Only up to 3 ViewHolders are visible.

Here's some mockup for better understanding of how the result looks.

enter image description here

I want to add ability to reposition cards in my RecyclerView so I created ItemTouchHelper.Callback and attached it to RecyclerView.

Once I longpress on any card I'm able to drag it to the left and right. When I move it near the screen edge RecyclerView automatically starts to scroll and then my view disappears.

From what I was able to find out I guess that it simply will get recycled - ItemTouchHelper#clearView(RecyclerView, RecyclerView$ViewHolder) is called and it originates from RecyclerView#removeDetachedView(View).

Is there any was I can bypass this behaviour?

ItemTouchHelper.Callback drag = new ItemTouchHelper.Callback() {

        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            int dragFlags = ItemTouchHelper.START | ItemTouchHelper.END | ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            return makeMovementFlags(dragFlags, ItemTouchHelper.ACTION_STATE_IDLE);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            int oldPosition = viewHolder.getAdapterPosition();
            int newPosition = target.getAdapterPosition();
            Log.d(getClass().getSimpleName(), String.format("View moved from %d to %d", oldPosition, newPosition));

            return true;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            // Not applicable
        }
    };

mView.recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
mView.recycler.setAdapter(mAdapter = new SimpleAdapter());

ItemTouchHelper touchHelper = new ItemTouchHelper(drag);
touchHelper.attachToRecyclerView(mView.recycler);

Stack from debugger when View disappears

mroczis
  • 1,879
  • 2
  • 16
  • 18
  • share your code. – Hemant Parmar May 22 '18 at 12:48
  • I hava met the same problem, once the item is recycled, clearView() will be invoked but I don't hope that happen, it seems that if I don’t call notifyItemMoved () in onMove() method this will happen, have you find any method to solve this problem? thanks! – cajsaiko Sep 07 '18 at 10:15

1 Answers1

0

In your ItemTouchHelper.Callback when you start dragging try to set

viewHolder.setIsRecyclable(false)

then when you end your dragging

viewHolder.setIsRecyclable(true)
firegloves
  • 5,581
  • 2
  • 29
  • 50
  • so have you tried to override clearView or removeDetachedView? – firegloves May 22 '18 at 14:47
  • ClearView is just a callback caused by removeDetachedView. Rewriting it will not help. The second option you suggest means that I should rewrite RecyclerView's layout methods which is imho way too dangerous. – mroczis May 22 '18 at 15:51
  • yeah it could be dangerous, but you could try to act as: if (! removing current touched view) super.removeDetachedView() . it's just an idea – firegloves May 22 '18 at 15:55