欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android RecyclerView实现滑动删除

程序员文章站 2022-06-25 11:55:24
本文实例为大家分享了recyclerview实现滑动删除的具体代码,供大家参考,具体内容如下package com.example.demo; import androidx.annotation.n...

本文实例为大家分享了recyclerview实现滑动删除的具体代码,供大家参考,具体内容如下

package com.example.demo;
 
import androidx.annotation.nonnull;
import androidx.appcompat.app.appcompatactivity;
import androidx.recyclerview.widget.itemtouchhelper;
import androidx.recyclerview.widget.linearlayoutmanager;
import androidx.recyclerview.widget.recyclerview;
 
import android.graphics.canvas;
import android.os.bundle;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.linearlayout;
import android.widget.textview;
 
import java.util.arraylist;
import java.util.list;
 
/**
 * @author huang xudong
 * @date 2020/7/26
 */
public class mainactivity extends appcompatactivity {
 private recyclerview recyclerview;
 
 private list list=new arraylist();
 
 class myadpter extends recyclerview.adapter<myadpter.viewholder>{
 
  class viewholder extends recyclerview.viewholder{
   private textview textview;
   private linearlayout linearlayout;
 
   public viewholder(@nonnull view itemview) {
    super(itemview);
    linearlayout=itemview.findviewbyid(r.id.ll_main);
    textview=itemview.findviewbyid(r.id.tv_main);
   }
  }
 
  @nonnull
  @override
  public viewholder oncreateviewholder(@nonnull viewgroup parent, int viewtype) {
   view inflate = layoutinflater.from(getapplicationcontext()).inflate(r.layout.item_demo,parent, false);
   return new viewholder(inflate);
  }
 
  @override
  public void onbindviewholder(@nonnull viewholder holder, int position) {
 
  }
 
  @override
  public int getitemcount() {
   return list.size();
  }
 }
 
 class callback extends itemtouchhelper.callback{
  @override
  public int getmovementflags(@nonnull recyclerview recyclerview, @nonnull recyclerview.viewholder viewholder) {
   return makemovementflags(0,itemtouchhelper.left);
  }
 
  @override
  public boolean onmove(@nonnull recyclerview recyclerview, @nonnull recyclerview.viewholder viewholder, @nonnull recyclerview.viewholder target) {
   return false;
  }
 
  @override
  public void onswiped(@nonnull recyclerview.viewholder viewholder, int direction) {
   /**
    * call max distance start onswiped call
    */
  }
 
  @override
  public void onchilddraw(@nonnull canvas c, @nonnull recyclerview recyclerview, @nonnull recyclerview.viewholder viewholder, float dx, float dy, int actionstate, boolean iscurrentlyactive) {
 
 
   if (actionstate==itemtouchhelper.action_state_swipe){
    /**
     * get {@link textview#getwidth()}
     */
    viewgroup viewgroup= (viewgroup) viewholder.itemview;
    textview textview = (textview) viewgroup.getchildat(1);
    viewgroup.layoutparams layoutparams = textview.getlayoutparams();
    if (math.abs(dx)<=layoutparams.width){
     /**
      * move {@link recyclerview.viewholder} distance
      */
     viewholder.itemview.scrollto((int) -dx,0);
     /**
      * callaction or register click bind view
      */
    }
   }
  }
 }
 
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
 
  recyclerview=findviewbyid(r.id.rv_main);
  list.add(1);
  list.add("2");
  myadpter myadpter=new myadpter();
  linearlayoutmanager linearlayoutmanager=new linearlayoutmanager(getapplicationcontext());
  linearlayoutmanager.setorientation(recyclerview.vertical);
  recyclerview.setlayoutmanager(linearlayoutmanager);
  recyclerview.setadapter(myadpter);
  itemtouchhelper itemtouchhelper=new itemtouchhelper(new callback());
  itemtouchhelper.attachtorecyclerview(recyclerview);
 }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".mainactivity">
 
 <androidx.recyclerview.widget.recyclerview
  android:id="@+id/rv_main"
  app:layout_constraintleft_toleftof="parent"
  app:layout_constraintright_torightof="parent"
  app:layout_constrainttop_totopof="parent"
  app:layout_constraintbottom_tobottomof="parent"
  android:layout_width="0dp"
  android:layout_height="0dp">
 
 </androidx.recyclerview.widget.recyclerview>
 
</androidx.constraintlayout.widget.constraintlayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:paddingbottom="10dp"
 xmlns:app="http://schemas.android.com/apk/res-auto">
 
 <linearlayout
  android:id="@+id/ll_main"
  android:background="@color/colorprimarydark"
  app:layout_constrainttop_totopof="parent"
  app:layout_constraintleft_toleftof="parent"
  app:layout_constraintright_torightof="parent"
  android:layout_width="0dp"
  android:layout_height="200dp"
  android:orientation="horizontal">
 
 </linearlayout>
 
 <textview
  android:textcolor="#ffffff"
  android:gravity="center"
  android:id="@+id/tv_main"
  android:textsize="34sp"
  android:text="删 除"
  android:background="@color/coloraccent"
  app:layout_constraintleft_torightof="@id/ll_main"
  app:layout_constrainttop_totopof="@id/ll_main"
  app:layout_constraintbottom_tobottomof="@id/ll_main"
  android:layout_width="180dp"
  android:layout_height="0dp">
 
 </textview>
 
 
 
</androidx.constraintlayout.widget.constraintlayout>

效果图:

Android RecyclerView实现滑动删除

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。