Android使用ItemTouchHelper实现侧滑删除和拖拽
作者:showdy 时间:2021-12-18 20:00:40
本文实例为大家分享了如何使用ItemTouchHelper实现侧滑删除和拖拽的具体代码,供大家参考,具体内容如下
1. 定义一个简单bean类:
public class ImgText {
public int resId;
public String des;
}
2. 实现一个RecyclerView.Adapter
public class SwipeRecyclerAdapter extends RecyclerView.Adapter<SwipeRecyclerAdapter.MyHolder> {
private List<ImgText> mList;
public SwipeRecyclerAdapter(List<ImgText> list) {
mList = list;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_swipe_recycler, parent, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
ImgText imgText = mList.get(position);
holder.img.setImageResource(imgText.resId);
holder.des.setText(imgText.des);
}
@Override
public int getItemCount() {
return mList.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
private ImageView img;
private TextView des;
public MyHolder(View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.img);
des = (TextView) itemView.findViewById(R.id.text);
}
}
}
3.绘制分割线:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
RecyclerView v = new RecyclerView(
parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition,
RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
4. 实现ItemTouchHelper.Callback接口:
public class SwipeCallback<T> extends ItemTouchHelper.Callback {
private RecyclerView.Adapter<? extends RecyclerView.ViewHolder> mAdapter;
private List<T> mList;
public SwipeCallback(RecyclerView.Adapter adapter, List<T> list) {
mAdapter = adapter;
mList = list;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlag=ItemTouchHelper.UP|ItemTouchHelper.DOWN;
int swipeFlags=ItemTouchHelper.END|ItemTouchHelper.START;
return makeMovementFlags(dragFlag,swipeFlags);
}
//用于移动item的位置
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
Collections.swap(mList, viewHolder.getAdapterPosition(), target.getAdapterPosition());
mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
//一般用于滑动删除
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mList.remove(viewHolder.getAdapterPosition());
mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
}
5.编写侧滑删除和拖拽Activity
public class SwipeRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<ImgText> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_recycler);
mRecyclerView= (RecyclerView) findViewById(R.id.recycler);
mList=new ArrayList<>();
for (int i = 0; i < 20; i++) {
ImgText it= new ImgText();
it.resId=R.mipmap.ic_launcher;
it.des=i+"侧滑删除文字";
mList.add(it);
}
final SwipeRecyclerAdapter adapter= new SwipeRecyclerAdapter(mList);
RecyclerView.LayoutManager manager= new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
mRecyclerView.setAdapter(adapter);
ItemTouchHelper swipeItemHelper= new ItemTouchHelper(new SwipeCallback<>(adapter,mList));
swipeItemHelper.attachToRecyclerView(mRecyclerView);
}
}
来源:https://blog.csdn.net/showdy/article/details/51578150
标签:Android,ItemTouchHelper,侧滑删除,拖拽
0
投稿
猜你喜欢
Flutter Zone异常处理方法及基本原理
2021-11-25 23:30:30
C# Word 类库的深入理解
2023-07-21 07:29:09
Android应用中利用ViewPager实现多页面滑动切换效果示例
2023-01-27 04:32:19
解决try-catch捕获异常信息后Spring事务失效的问题
2022-11-15 03:17:33
C#中Json反序列化的实现方法
2022-07-16 04:34:19
Java中list.contains()的用法及拓展
2022-11-06 21:04:59
Android编程使用android-support-design实现MD风格对话框功能示例
2022-05-19 03:46:39
Java 数据结构进阶二叉树题集下
2022-07-11 19:16:18
浅谈对Java双冒号::的理解
2023-09-20 02:19:21
解析C#中的私有构造函数和静态构造函数
2021-11-27 07:13:15
java对象序列化与反序列化的默认格式和json格式使用示例
2021-12-07 18:22:23
Android中自定义View的实现方式总结大全
2023-02-03 08:55:55
C#日期格式化的几个要点小结
2023-12-19 08:56:05
Java Fluent Mybatis 项目工程化与常规操作详解流程篇 下
2021-07-19 11:33:11
java 解决Eclipse挂掉问题的方法
2023-02-19 14:05:55
Java Web开发之基于Session的购物商店实现方法
2023-06-09 20:46:35
Java使用Jedis操作Redis服务器的实例代码
2023-09-08 07:38:42
java.lang.OutOfMemoryError 错误整理及解决办法
2021-09-23 04:09:51
C#设置自定义文件图标实现双击启动(修改注册表)
2023-08-21 08:43:46
Java的二叉树排序以及遍历文件展示文本格式的文件树
2021-08-04 07:13:43