SoFunction
Updated on 2025-03-01

Android RecyclerView implements click entry deletion

This article shares the specific code for RecyclerView to delete clicks for your reference. The specific content is as follows

public class MainActivity extends AppCompatActivity implements {

  private Button mButton1;
  private Button mButton2;
  private Button mButton3;
  private Button mButton4;
  private Button mButton5;
  private RecyclerView mRecyclerView;
  private ArrayList<String> mList;
  private LinearLayoutManager mLinearLayoutManager;
  private RvAdapter mAdapter;


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

    findViews();

    mList = new ArrayList<>();
    for (int i=0;i<20;i++){
      (i+"item");
    }

    mAdapter = new RvAdapter(mList, this);
    (mAdapter);

    //Set the split line    (new DividerItemDecoration(this,));
    //Set the default layout    mLinearLayoutManager = new LinearLayoutManager(this, , false);
    (mLinearLayoutManager);

    (new () {
      @Override
      public void onItemClick(int position) {
        (position);
      }

      @Override
      public void onItemLongClick(int position) {

        (position);
      }
    });

  }

  private void findViews() {

    mRecyclerView = findViewById();

    mButton1= findViewById(.b1);
    mButton2= findViewById(.b2);
    mButton3= findViewById(.b3);
    mButton4= findViewById(.b4);
    mButton5= findViewById(.b5);

    (this);
    (this);
    (this);
    (this);
    (this);


  }

  @Override
  public void onClick(View view) {
    switch (()){
      case .b1:

        (3);
        (0);
        break;
      case .b2:

        (()-1);
        break;
      case .b3:

        mLinearLayoutManager = new LinearLayoutManager(this, , false);
        (mLinearLayoutManager);
        break;
      case .b4:

        (new GridLayoutManager(this, 3));
        //(new .(this, ));

        break;
      case .b5:

        (new StaggeredGridLayoutManager(2,));
        break;
    }
  }
}

activity_main.xml

< xmlns:andro
  xmlns:app="/apk/res-auto"
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      <Button
        android:
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Add to"/>
      <Button
        android:
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="delete"/>
      <Button
        android:
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="List"/>
      <Button
        android:
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Grid"/>
      <Button
        android:
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="flow"/>
    </LinearLayout>
    <.
      android:
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
  </LinearLayout>

</>

public class RvAdapter extends <>{

  private List<String> lists;
  private Context mContext;

  public RvAdapter(List<String> lists, Context context) {
     = lists;
    mContext = context;
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = (mContext, , null);
    ViewHolder holder = new ViewHolder(view);
    return holder;
  }

  public void addData(int position) {
    (position,"ff");
    notifyItemInserted(position);
  }

  public void remove(int i) {
    (i);
    notifyItemRemoved(i);
    notifyDataSetChanged();

  }

  public interface OnItemClickListener{  //Custom interface callback setting click event    void onItemClick(int position);
    void onItemLongClick(int position);
  }

  private OnItemClickListener mOnItemClickListener;

  public void setOnItemClickListener(OnItemClickListener onItemClickListener){
    mOnItemClickListener=onItemClickListener;
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {
    ((position));

       (new () {
         @Override
         public void onClick(View view) {

           int ps = ();
           (ps);
         }
       });

       (new () {
         @Override
         public boolean onLongClick(View view) {

           int ps=();
           (ps);
           return false;
         }
       });

  }


  @Override
  public int getItemCount() {
    return ();
  }

  public static class ViewHolder extends {
    public final TextView mTextView;

    public ViewHolder(View itemView) {
      super(itemView);
      mTextView = (TextView) ();
    }
  }
}

implementation ':recyclerview-v7:27.1.1'

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.