ItemTouchHelper notifyItemMoved from to position not working
Asked Answered
T

1

6

Current,I am having a problem when execute drag and drop item in recyclerview. I am doing with reference from https://github.com/iPaulPro/Android-ItemTouchHelper-Demo But when execute function in adapter:

mListBookMark is ArrayList of Object

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    Collections.swap(mListBookMark, fromPosition, toPosition);
    notifyItemMoved(fromPosition, toPosition);
    return true;
}

When I drag item from position a to position b but when finish drag recycler view not data changed. How must I do? Please give some suggestion for me! Thank you.

Teeming answered 7/3, 2016 at 11:28 Comment(1)
I have this problem too. For now I solve putting notifyDataSetChanged() before return true.Ornithopod
L
8

Try adding notifyItemChanged() to your code, like this:

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    Collections.swap(mListBookMark, fromPosition, toPosition);
    notifyItemMoved(fromPosition, toPosition);
    notifyItemChanged(fromPosition);
    notifyItemChanged(toPosition);
    return true;
}

This should update the views based on their new position.

Lo answered 5/10, 2016 at 15:8 Comment(1)
This actually messes up the animation quite badly and somehow causes onItemMove to be called twice!Hearts

© 2022 - 2024 — McMap. All rights reserved.