I implemented the reorder of items in recyclerView using itemTouchHelper.callback as it seems to be the most popular solution now. However this allows to reorder only after long press on any item and I need to be able to freely move items around any time (I attach item itemTouchHelper for 'edit mode' and detach right after. What is the easiest way to achieve that?
1 Answer
first of all disable LongPressDragEnabled in itemTouchHelper and then just call startDrag(RecyclerView.ViewHolder) from on touch of your custom handle view ie Imageview or anything like this
viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mItemTouchHelper.startDrag(viewHolder);
}
return false;
}
});
-
Thank you. I see one mismatch thing here: dragButton is in item but itemTouchHelper is in recyclerView. So should I call this from item and expose to the recycler to it? Apr 27, 2018 at 9:25
-
you can pass recycler view refrence in adapter or you can also use interface as well to get callback in activity/fragment Apr 27, 2018 at 9:31
-
-
I did this in adapter. I see onTouch calls in debugger and that viewHolder is not null but dragging doesn't actually start. Of course I disabled LongPressDrag. Apr 29, 2018 at 5:33