当前位置:网站首页>Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting

Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting

2022-04-23 14:09:00 Senzhiqianshou

Let's take a look at the renderings to be realized :
![ Simple vertical drag and drop sorting ](https://img-blog.csdnimg.cn/20210329142737702.gif#pic_center
The effect is simple , It's a vertical list , You can then drag its children to sort .
Therefore, the method adopted is still RecyclerView+ItemTouchHelper, About ItemTouchHelper You can also refer to RecyclerView Advanced use ( One )- Simple implementation of sideslip deletion and RecyclerView A detailed study -RecyclerView Discussion and repair of click dislocation . Here we need to write a class by ourselves VerticalDragSortHelperCallBack To inherit ItemTouchHelper.Callback, We need to implement the following methods :

	@Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
    
        int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        return makeMovementFlags(dragFlag, ItemTouchHelper.ACTION_STATE_IDLE);
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
    
        int fromPos = viewHolder.getAdapterPosition();
        int toPos = target.getAdapterPosition();
        Log.d("onMove", fromPos + "--->" + toPos);
        //1、 Where to exchange entries from the data source 
        Collections.swap(recyclerItemList, fromPos, toPos);
        //2、 Notify the refresh view of changes from the view 
        recyclerView.getAdapter().notifyItemMoved(fromPos, toPos);

        //3、 Correct the actually clicked position, Prevent when clicking ,position Disorder 
        int startPos = Math.min(fromPos, toPos);
        int itemCount = Math.abs(fromPos - toPos) + 1;
        recyclerView.getAdapter().notifyItemRangeChanged(startPos, itemCount);

        //4、PS:2 and 3 The fitting step can be used 4 replace , But there is no replacement animation , And the refresh efficiency is lower than the above , Is a global refresh 
// recyclerView.getAdapter().notifyDataSetChanged();

        // Notice the return value : Only in return true When , To leave onMoved Method 
        return true;
    }

    @Override
    public void onMoved(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, int fromPos, @NonNull RecyclerView.ViewHolder target, int toPos, int x, int y) {
    
        super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
        Log.d("onMoved", " first :" + recyclerItemList.get(0).toString() + " " + fromPos + "--->" + toPos);
        Log.d("onMoved", " the second :" + recyclerItemList.get(1).toString() + " " + fromPos + "--->" + toPos);
        if (onDragListener != null) {
    
            onDragListener.onItemMoved(viewHolder, target, fromPos, toPos);
        }
    }

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

It should be noted that :
1、 Because drag vertically , therefore getMovemonetFlags The return is DOWN and UP
2、 stay onMove in ,pos Because it is calculated from zero , So the actual number of refresh entries should be +1
3、 stay onMove in , About data exchange , The system is adopted Collections Of swap In exchange for , More efficient
4、isLongPressDragEnabled Indicates whether dragging is enabled after long pressing , We need to go back to true, Indicate need

Source code download address :github

版权声明
本文为[Senzhiqianshou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231405037238.html