Android RecyclerView的使用与添加点击事件

2017-08-07  本文已影响0人  MartinDong

RecyclerView使用

RecyclerView 是 Android Loillpop (5.0)版本中新添加的一个用来取代 ListView 的 SDK。

1. 简介

RecyclerView 与 ListView 原理是类似的:都是仅仅维护少量的View展示大量的数据。
关系图

2. 使用的步骤

2.1 添加依赖

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

2.2 编写代码

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

         <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="45dp"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:layout_marginBottom="20dp">
        </android.support.v7.widget.RecyclerView>

    </RelativeLayout>

3. 运行

RecyclerView 最大的特点就是灵活,可以自定义展示方式。

简单区分一下布局:
1.mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    这是横向布局
2. mLayoutManager = new GridLayoutManager(context,columNum);
    mRecyclerView.setLayoutManager(mLayoutManager);
    这是 Grid 布局。
3. StaggeredGridLayoutManager
    这是瀑布流布局

4. 给 RecyclerView 添加点击事件

给 RecyclerView 添加点击事件,最好是在adapter中添加
其实就是使用观察者设计模式去写的,有时间整理一下观察者设计模式。

ok,已经可以使用RecyclerView了。

上一篇下一篇

猜你喜欢

热点阅读