使用RecyclerView实现滑动添加、滑动删除,以及瀑布流、ListView、GridView的转换

本文介绍了如何在Android Studio中使用RecyclerView实现滑动添加和滑动删除功能,并展示了将RecyclerView转换为瀑布流、ListView及GridView的方法。通过添加依赖库和设置布局文件,详细解析了MainActivity.java中的关键代码。

IDE:AndroidStudio

首先:添加依赖库

compile 'com.android.support:recyclerview-v7:23.4.0'

效果图如下:

效果图

本例用到的类
类名
说明
RecyclerView.Adapter 托管数据集合,为每个Item创建视图
RecyclerView.ViewHolder 承载Item视图的子视图
RecyclerView.LayoutManager 负责Item视图的布局
RecyclerView.ItemDecoration 给Item进行各种装饰
DefaultItemAnimator 负责添加、删除数据时的动画效果


本例自定义的类
类名
说明
RecyclerAdapter 托管数据集合,为每个Item创建视图
CustomViewHolder 承载Item视图的子视图
CustomCallback 负责Item触摸事件
CustomItemDecoration 用来绘制divider


本例自定义的接口
类名
说明
IOnRecyclerViewItemClickListener 实现 RecyclerView的item点击事件
IRecyclerViewItemChange 实现RecyclerView的 item 改变


布局文件
1. main_activity

<?xml version="1.0" encoding="utf-8"?>
<!--main_activity-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_listViewVer_activity_main"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="列表(竖)"/>

        <Button
            android:id="@+id/btn_GridViewVer_activity_main"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="网格(竖)"/>

        <Button
            android:id="@+id/btn_staggeredGridVer_activity_main"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="瀑布(竖)"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_listViewHor_activity_main"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="列表(横)"/>

        <Button
            android:id="@+id/btn_GridViewHor_activity_main"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="网格(横)"/>

        <Button
            android:id="@+id/btn_staggeredGridHor_activity_main"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="瀑布(横)"/>
    </TableRow>

    <android.support.v7.widget.RecyclerView
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值