RecyclerView基本使用

简介: 博客地址背景以前在展示列表时一直都是在使用ListView。唯一的区别大概就是适配器Adapter的不同而已。但是后来接触到了RecyclerView之后,就喜欢上了RecyclerView。

博客地址

背景

以前在展示列表时一直都是在使用ListView。

唯一的区别大概就是适配器Adapter的不同而已。

但是后来接触到了RecyclerView之后,就喜欢上了RecyclerView。

因此本篇打算说下RecyclerView的基本使用,给大家参考下。

正所谓“学以致用”,验明是否掌握了一门东西的最好方法是能够把这个东西讲给别人听,别人还能听懂。

所以基于此,本篇文章应运而生。

RecyclerView的好处

这里我就截取官网上的两段文字来说明。

Many apps need to display user-interface elements based on large data sets, or data that frequently changes. For example, a music app might need to display information about thousands of albums, but only a dozen of those albums might be on-screen at a time. If the app created UI widgets for each of those albums, the app would end up using a lot of memory and storage, potentially making the app slow and crash-prone. On the other hand, if the app created UI widgets each time a new album scrolled onto the screen and destroyed the widgets when it scrolled off, that would also cause the app to run slowly, since creating UI objects is a resource-intensive operation.

To address this common situation, the Android Support Library provides the RecyclerView suite of objects. RecyclerView and its associated classes and interfaces help you to design and implement a dynamic user interface that runs efficiently. You can use these classes as they are, or customize them to suit your specific needs.

传送门

A flexible view for providing a limited window into a large data set.

传送门

简单的说RecyclerView在显示大量数据的时候能起到高效的作用。

实战

前面铺垫了那么多,接下来我们就实际看看一个最简单的RecyclerView如何撰写吧?

首先说明下RecyclerView的几大组成部分:

  • RecyclerView本身
  • 适配器Adapter(下面的例子为MainAdapter)
  • ViewHolder(下面的例子为MainViewHolder)
  • Item布局(下面的例子为item_main.xml)

这四个组成部分依赖关系是Item->ViewHolder->Adapter->RecyclerView

好了,接下来就让我们一步一步来实现一个展示文本的列表。

  1. 添加gradle依赖。在要使用的module的build.gradle文件中加入依赖
compile 'com.android.support:recyclerview-v7:26.+'
  1. 确定列表布局item_main.xml。这里列表展示的就是一个文本,所以只需要一个TextView即可。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/txt_title"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_50"
    android:gravity="center"
    >
</TextView>
  1. 创建列表布局的ViewHolder,继承自RecyclerView.ViewHolder,并初始化列表中的控件。
public class MainViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.txt_title)
    TextView mTxtTitle;

    public MainViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

}

这里使用了butterknife对TextView进行初始化。

  1. 创建适配器Adapter,继承自RecyclerView.Adapter<MainViewHolder> 。将数据源和布局文件通过Adapter联系起来。
public class MainAdapter extends RecyclerView.Adapter<MainViewHolder> {

    private Activity activity;
    private List<String> itemList;

    public MainAdapter(Activity activity, List<String> itemList) {
        if (activity == null || itemList == null) {
            throw new IllegalArgumentException("params can't be null");
        }
        this.activity = activity;
        this.itemList = itemList;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        holder.mTxtTitle.setText(itemList.get(position));
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }
}

这里说明下几个方法:

  • 构造函数。用于对数据源进行初始化,以及传递activity进来。
  • onCreateViewHolder。使用item布局来初始化MainViewHolder。
  • onBindViewHolder。通过MainViewHolder获取到具体控件执行对应逻辑。
  • getItemCount。返回数据源的大小。
  1. 创建RecyclerView并进行初始化。

在使用RecyclerView的布局文件中加入下面代码:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false" />

初始化RecyclerView并将其与适配器绑定。

由于这里使用了butterknife,所以创建的时候就初始化了。

@BindView(R.id.recycler_view)
RecyclerView mRecyclerView;
private void initRecyclerView() {
        //设置子视图
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //添加分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        //设置适配器
        mAdapter = new MainAdapter(this, mItemList);
        mRecyclerView.setAdapter(mAdapter);
    }

如此,一个简单的RecycleView就完成了。


心动不如行动,赶紧自己去敲下代码熟悉下呗~
代码传送门

相关文章
|
12月前
|
存储 网络协议 数据挖掘
阿里云服务器通用算力型u1实例2核4G5M配置199元一年怎么样?实例性能与适用场景参考
阿里云目前特惠价格只要199元一年的云服务器怎么样?对于部分还未使用过这款云服务器的企业用户来说,并不是很清楚这款云服务器的实例规格性能与适用场景情况,本文为大家详细介绍这款云服务器的具体配置、可选地域、以及u1实例的性能和适用场景,以供大家更详细的了解这款云服务器,从而根据自己的需要作出选择参考。
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16916 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1245 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2334 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3805 22