The strange behavior of drag&drop using ItemTouchHelper and RecyclerView. How to fix?

I'm trying to use RecyclerView with very convenient for my purposes ItemTouchHelper.
All I want is a simple script “drag & drop” and “swipe-to-dismiss”.
Swipe is working correctly, items are removed in the adapter correctly, the animation is also what is happening, but “drag & drop” works very strange. After a long click, the item will not move, although the selection is triggered, and the adapter method is called onItemMove.

Here is a strange behavior in my RecyclerView
59d21fcab04c1118769817.gif

And that's what I'm trying to achieve
59d21ff4ce99d680525653.gif

Here is the code of the presenter, onSelectedChanged and clearView here only for clarity, selecting.

addedCitiesRecycler.setLayoutManager(llmAddedCities);

 adapterAddedCities = new AdapterAddedCities();


 ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN ItemTouchHelper.START | ItemTouchHelper.END) {
@Override
 public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
 adapterAddedCities.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
 return true;
}

@Override
 public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
adapterAddedCities.onItemDismiss(viewHolder.getAdapterPosition());
}

@Override
 public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
 if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
 AdapterAddedCities.ViewHolder holder = (AdapterAddedCities.ViewHolder) viewHolder;
holder.itemView.setBackgroundColor(Color.LTGRAY);
}
 super.onSelectedChanged(viewHolder, actionState);
}

@Override
 public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
 AdapterAddedCities.ViewHolder holder = (AdapterAddedCities.ViewHolder) viewHolder;
holder.itemView.setBackgroundColor(0);
}
};

 itemTouchHelper = new ItemTouchHelper(callback);
 itemTouchHelper.attachToRecyclerView(addedCitiesRecycler);


Here are a couple of methods of the adapter that changes the data set and notifying RecyclerView of changes.

public void onItemDismiss(int position) {
citiesData.remove(position);
notifyItemRemoved(position);
}

 public void onItemMove(int fromPosition, int toPosition) {
 CityInfo tmp = citiesData.remove(fromPosition);
 citiesData.add(toPosition > fromPosition ? toPosition - 1 : toPosition, tmp);
 notifyItemMoved(fromPosition, toPosition);
 }


I started the project from
article
everything works fine, I do not understand what could be the problem.

Maybe I did not consider something c focus on the movable element or de-emphasizing?
June 14th 19 at 21:00
1 answer
June 14th 19 at 21:02
Solution
I updated com.android.support:recyclerview-v7:26.0.0-alpha1 to com.android.support:recyclerview-v7:27.0.0 in the project and it worked perfectly.

Most likely it was a bug in the library, so if you are faced with a similar problem, just obnoviti to the latest version and check how it behaves the RecyclerView.

Find more questions by tags AndroidJava