RecycleView realizes item sideslip deletion and drag

  • 2021-12-09 10:14:37
  • OfStack

In this paper, we share the specific code of RecycleView to realize item sideslip deletion and drag for your reference. The specific contents are as follows

1. Add dependencies


//  Side slip deletion dependency 
implementation 'com.yanzhenjie.recyclerview:support:1.3.2'

2. Layout files

The controls used here are: com. yanzhenjie. recyclerview. SwipeRecyclerView


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">
 
 <com.yanzhenjie.recyclerview.SwipeRecyclerView
  android:id="@+id/srv"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

</RelativeLayout>

3. Code implementation


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.yanzhenjie.recyclerview.touch.OnItemMoveListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {
 SwipeRecyclerView srv; // Control 
 List<JavaBean.DataBean> dataAll = new ArrayList<>(); // A collection of stored data 
 MyAdapter myAdapter; // Adapter 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  // Initialize control 
  initView();

  // Get data ,  Data can be simulated ,  Not on display 
  initData();

  // Monitor events 
  initListener();
 }

 // TODO 3  Monitor events 
 private void initListener() {
  srv.setItemViewSwipeEnabled(true); // Sideslip deletion   Default off 
  srv.setLongPressDragEnabled(true); // Drag-and-drop sort   Default off 
  //  Drag-and-drop monitor 
  srv.setOnItemMoveListener(new OnItemMoveListener() {
   //  This method is used in the Item Called when the swap position is dragged and dropped. 
   @Override
   public boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) {
    //  No. 1 1 Parameters are to be exchanged for Item , No. 2 Of the target location Item . 
    int adapterPosition = srcHolder.getAdapterPosition();
    int adapterPosition1 = targetHolder.getAdapterPosition();
    // swap Exchange data and update adapter . 
    Collections.swap(dataAll, adapterPosition, adapterPosition1);
    myAdapter.notifyItemMoved(adapterPosition, adapterPosition1);

    return true;//  Return true , indicating that the data exchange is successful, ItemView You can swap places. 
   }

   //  This method is used in the Item Called on sideslip deletion. 
   @Override
   public void onItemDismiss(RecyclerView.ViewHolder srcHolder) {
    //  Removes the Item Corresponding data, and refresh Adapter . 
    int position = srcHolder.getAdapterPosition();
    dataAll.remove(position);
    myAdapter.notifyItemRemoved(position);
   }
  });
 }

 //TODO 2  Get data 
 private void initData() {
  // The data obtained by callback here 
  OkgoData.getInstance().getDataByOkGo(new DataCallBack() {
   @Override
   public void getDataJson(String json) {
    List<JavaBean.DataBean> data = new Gson().fromJson(json, JavaBean.class).getData();
    dataAll.clear();
    dataAll.addAll(data);
    myAdapter.setNewData(dataAll);
    myAdapter.notifyDataSetChanged(); // Adapter notification update 
   }
  });
 }

 //TODO 1  Initialize control 
 private void initView() {
  srv = findViewById(R.id.srv); // Find the control 

  //  Add an underline 
  DividerItemDecoration decoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
  srv.addItemDecoration(decoration);
  // Add adapter 
  myAdapter = new MyAdapter(R.layout.layout_adapter_item, dataAll);
  srv.setAdapter(myAdapter);

  // Add layout management   It must be added or it cannot be displayed 
  LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
  srv.setLayoutManager(layoutManager);
 }
}

For other functions, please refer to: RecycleView sideslip deletion and drag


Related articles: