The renderings are as follows:

ItemTouchHelper helper = new ItemTouchHelper();
helper.attachToRecyclerView(recyclerView);
Copy the code

Yes, it is a line of code to achieve the effect you want!

ItemTouchHelper is described in more detail below

ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
        Both linear and grid layouts can be used
        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            int dragFrlg = 0;
            if (recyclerView.getLayoutManager() instanceof GridLayoutManager){
                dragFrlg = ItemTouchHelper.UP|ItemTouchHelper.DOWN|ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
            }else if(recyclerView.getLayoutManager() instanceof LinearLayoutManager){
                dragFrlg = ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
            }
            return makeMovementFlags(dragFrlg,0);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            This method is called back each time the Position of the dragged viewHolder changes
            int fromPosition = viewHolder.getAdapterPosition();
            // Get the viewHolder of the currently dragged item
            int toPosition = target.getAdapterPosition();
            if (fromPosition < toPosition ) {
                if (toPosition < d.size() - 1) {// This indicates that the last item is not replaceable, usually the last item is to add more images +
                    for (int i = fromPosition; i < toPosition; i++) {
                        Collections.swap(datas, i, i + 1); } ap.notifyItemMoved(fromPosition, toPosition); }}else {
                for (int i = fromPosition; i > toPosition; i--) {
                    Collections.swap(datas, i, i - 1);
                }
                ap.notifyItemMoved(fromPosition, toPosition);
            }
            return true;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
              // Sideslip delete can be used;
        }

        @Override
        public boolean isLongPressDragEnabled(a) {
            return true;
        }
        /** * long highlight is called when an Item is selected@param viewHolder
         * @param actionState
         */
        @Override
        public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
            if(actionState ! = ItemTouchHelper.ACTION_STATE_IDLE) { viewHolder.itemView.setBackgroundColor(Color.RED); viewHolder.itemView.setScaleX(1.2 f);
                viewHolder.itemView.setScaleY(1.2 f);
                // Get system vibration service // vibration 70 ms
// Vibrator vib = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
// vib.vibrate(70);
            }
            super.onSelectedChanged(viewHolder, actionState);
        }

        /** * Restore highlight when finger is released *@param recyclerView
         * @param viewHolder
         */
        @Override
        public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            super.clearView(recyclerView, viewHolder);
            viewHolder.itemView.setBackgroundColor(Color.parseColor("#21f1f1"));
            viewHolder.itemView.setScaleX(1.0 f);
            viewHolder.itemView.setScaleY(1.0 f);
            ap.notifyDataSetChanged();  // Refresh the adapter after the drag is complete so that the drag deletions are not messed up}});Copy the code

At the end of the article

The DEMO link