Android RecyclerView实现滑动删除
程序员文章站
2022-03-20 18:46:09
本文实例为大家分享了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>
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 基于Vue实现微前端的示例代码
推荐阅读
-
android开发教程之实现滑动关闭fragment示例
-
Android中创建快捷方式及删除快捷方式实现方法
-
Android Studio 使用ViewPager + Fragment实现滑动菜单Tab效果 --简易版
-
Android使用RecyclerView实现今日头条频道管理功能
-
Android 滑动返回Activity的实现代码
-
Android中判断listview是否滑动到顶部和底部的实现方法
-
Android自定义View实现等级滑动条的实例
-
Android GestureDetector实现手势滑动效果
-
Android Recyclerview实现多选,单选,全选,反选,批量删除的功能
-
Android使用Recyclerview实现图片水平自动循环滚动效果