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
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#F4F4F3"
- android:orientation="vertical"
- tools:context="com.handsome.app3.MainActivity">
-
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/top" />
-
- <android.support.v7.widget.RecyclerView
- android:id="@+id/rv"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1" />
-
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/bottom" />
- </LinearLayout>
步骤三:创建一个javaBean,存储item的信息
- public class Message {
-
- private String username;
- private String time;
- private String message;
- private int img_id;
-
- public Message(String username, String time, String message, int img_id) {
- this.username = username;
- this.time = time;
- this.message = message;
- this.img_id = img_id;
- }
-
- public String getUsername() {
- return username;
- }
-