ItemTouchHelper - The drop is forced after the first jumped line

让人想犯罪 __ 提交于 2020-01-14 09:42:08

问题


I am using ItemTouchHelper and ItemTouchHelper.SimpleCallback to allow the user to reorder a vertical list Recycler View.

The drag and drop works but the drop is forced after the first jumped line even though I don't leave up my finger from the dragged cell.

Please find below the SimpleCallback :

private void initSwipeAndDrap() {

    ItemTouchHelper.SimpleCallback simpleItemTouchCallback =
            new ItemTouchHelper.SimpleCallback(
                    ItemTouchHelper.UP | ItemTouchHelper.DOWN,
                    0) {

                //========== Swipe (Not used) ==============

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


                //========== Drag ==============

                @Override
                public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {

                    int fromPosition = viewHolder.getAdapterPosition();
                    int toPosition = target.getAdapterPosition();

                    Podcast podcast = rva.podcasts.remove(fromPosition);
                    rva.podcasts.add(toPosition, podcast);
                    act.dmo.updatePodcastsListPosition();
                    act.dmo.notifyDataSetChangedPodcast();

                    return true;
                }

                @Override
                public boolean isLongPressDragEnabled() {
                    return false;
                }


            };

    itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(rv);
}

回答1:


Replacing notifyDataSetChanged() by notifyItemMoved() in onMove() method fixed my problem.

Which supprised me as I thought that onMove() was called after the drop. In fact, it has an effect on the drop itself.




回答2:


For those having the snaping problem when you move an element, if you are using this override

   @Override
    public int getItemViewType(int position) {
        return position;
    }

Just delete it from your adapter and it will work just fine



来源:https://stackoverflow.com/questions/38666255/itemtouchhelper-the-drop-is-forced-after-the-first-jumped-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!