Android RecyclerView实现滑动删除
作者:夜行侠~@ 时间:2022-11-20 19:50:42
本文实例为大家分享了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>
效果图:
来源:https://blog.csdn.net/s452195377/article/details/107593732
标签:RecyclerView,滑动删除
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring异步service中处理线程数限制详解
2021-09-02 23:20:12
Qt实现计算器功能
2022-07-29 06:30:55
![](https://img.aspxhome.com/file/2023/1/124061_0s.jpg)
基于Ant路径匹配规则AntPathMatcher的注意事项
2021-11-19 03:58:16
![](https://img.aspxhome.com/file/2023/9/76629_0s.png)
基于Java Socket实现一个简易在线聊天功能(一)
2023-09-23 17:18:35
Android应用程序四大组件之使用AIDL如何实现跨进程调用Service
2022-02-03 13:24:15
![](https://img.aspxhome.com/file/2023/4/122224_0s.png)
android轻量级无侵入式管理数据库自动升级组件
2023-12-09 15:36:01
![](https://img.aspxhome.com/file/2023/4/130334_0s.png)
C语言二叉树的非递归遍历实例分析
2022-02-08 02:41:15
Android Universal ImageLoader 缓存图片
2023-03-04 05:16:54
SpringCloud使用Feign实现远程调用流程详细介绍
2021-06-02 06:47:16
![](https://img.aspxhome.com/file/2023/5/79875_0s.png)
Android6.0蓝牙出现无法扫描设备或闪退问题解决办法
2023-06-03 13:36:27
![](https://img.aspxhome.com/file/2023/5/139605_0s.jpg)
详解Android的内存优化--LruCache
2022-07-18 14:28:41
![](https://img.aspxhome.com/file/2023/9/126779_0s.gif)
Android Studio打包jar及aar包的方法
2023-06-18 15:59:53
![](https://img.aspxhome.com/file/2023/2/82392_0s.jpg)
详解IDEA多module项目maven依赖的一些说明
2023-07-21 06:09:05
![](https://img.aspxhome.com/file/2023/4/67974_0s.png)
Android音视频开发Media FrameWork框架源码解析
2021-08-07 05:12:14
![](https://img.aspxhome.com/file/2023/3/114303_0s.jpg)
Java之String、StringBuffer、StringBuilder的区别分析
2022-12-29 10:38:05
Android图片处理实例分析
2022-09-10 20:34:50
C#控制台程序中使用官方依赖注入的实现
2021-07-12 15:35:09
![](https://img.aspxhome.com/file/2023/2/83342_0s.png)
proguar在Android混淆中的用法
2021-10-01 01:12:12
Java实现手写线程池实例并测试详解
2022-03-04 11:23:27
![](https://img.aspxhome.com/file/2023/6/122276_0s.jpg)
Android USB转串口通信开发实例详解
2022-01-05 15:28:17