Android实现RecyclerView侧滑删除和长按拖拽

本文介绍了如何在Android中使用RecyclerView实现拖拽排序和侧滑删除功能。通过导入RecyclerView,创建JavaBean,设置布局和触摸事件,编写适配器,最后添加触摸事件处理来实现这一效果。示例代码展示了线性布局和网格布局两种情况下的实现方式。
摘要由CSDN通过智能技术生成

RecyclerView实现拖拽排序侧滑删除效果


事先说明:

RecyclerView是ListView的升级版,使用起来比ListView更规范,而且功能和动画可以自己添加,极容易扩展,同样也继承了ListView复用convertView和ViewHolder的优点。


思路分析:

1、导包、在布局中使用RecyclerView

2、需要一个JavaBean用来存储展示信息

3、需要一个填充RecyclerView的布局文件

4、在代码中找到RecyclerView,并为其绑定Adapter和触摸事件

5、适配器的编写、触摸事件的处理

总体上跟ListView是一样的,就是换了一种代码风格


思路图解:



效果一(线性布局):                                                                         效果二(网格布局):

   


步骤一:RecyclerView是在support.v7包,Android Studio则是recyclerview这个包



步骤二:创建布局文件,使用recyclerview

[java] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#F4F4F3"  
  6.     android:orientation="vertical"  
  7.     tools:context="com.handsome.app3.MainActivity">  
  8.   
  9.     <ImageView  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="@drawable/top" />  
  13.   
  14.     <android.support.v7.widget.RecyclerView  
  15.         android:id="@+id/rv"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="match_parent"  
  18.         android:layout_weight="1" />  
  19.   
  20.     <ImageView  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:background="@drawable/bottom" />  
  24. </LinearLayout>  

步骤三:创建一个javaBean,存储item的信息

[java] view plain copy
  1. public class Message {  
  2.   
  3.     private String username;  
  4.     private String time;  
  5.     private String message;  
  6.     private int img_id;  
  7.   
  8.     public Message(String username, String time, String message, int img_id) {  
  9.         this.username = username;  
  10.         this.time = time;  
  11.         this.message = message;  
  12.         this.img_id = img_id;  
  13.     }  
  14.   
  15.     public String getUsername() {  
  16.         return username;  
  17.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值