android - recycleView item on drop is not getting sorted properly with sqlite

前端 未结 2 834
遥遥无期
遥遥无期 2021-01-21 18:48

I have implemented a recycle view On drag & drop the sorting item in the recycle view. But sorting is not accurate, sorting not working properly. I tried write below code

2条回答
  •  囚心锁ツ
    2021-01-21 19:35

    
    @Overid
    private void onDrop(int from, int to) {   // this listener only calls when item Droped after drag
        ExerciseDetails moveItem = items.remove(from);
        items.add(moveItem, to)
        int minPosition = from > to ? to : from;
        int maxPosition = from > to ? from : to;
        // now the item index is the sort index of ExerciseDetails
        for (int i = minPosition; i <= maxPosition; i++) {
            ExerciseDetails item = items.get(i);
            item.setOrder(i);
            exercisesDAO.updateOrder(i, item.getExDetailsId());
        }
        // notify recyclerview to refresh the position of item dragged
        adapter.notifyItemMoved(from, to)
    }
    

提交回复
热议问题