Android开发中如何使用RecyclerView
介绍 在Android应用程序中,只要您想显示数据列表,就可以使用 RecyclerView 。 早期的Android提供 ListView 了同样的东西。 RecyclerView 可以被认为是一个更高级和性能优化的版本 ListView 。 顾名思义, RecyclerView 也可以使用 ViewHolder 模式 回收物
介绍
在Android应用程序中,只要您想显示数据列表,就可以使用RecyclerView
。早期的Android提供ListView
了同样的东西。RecyclerView
可以被认为是一个更高级和性能优化的版本ListView
。顾名思义,RecyclerView
也可以使用ViewHolder
模式回收物品。除此之外,它还使用了一个LayoutManager
更灵活的方法来创建水平,垂直甚至交错的列表。它还提供垂直和水平滚动以及项目动画。虽然ListView
也可以修改为使用' ViewHolder
'模式,RecyclerView
强制使用相同的模式。它甚至可以处理大量数据,同时提供响应式界面。
在这篇文章中,我们将看到如何RecyclerView
使用Android Studio在Android应用程序中实现一个简单的。我将使用Android studio版本。
创建一个新项目
- 打开Android Studio。转到文件 - >“新建项目”,填写应用程序名称和其他详细信息,选择API版本为API 15。选择清空活动,在下一个屏幕中保留默认选项,然后单击完成。将创建新项目以及所需文件。(在示例中,我将应用程序名称指定为“
SimpleRecyclerViewExample
”并将包装为“com.example
” - 要使用
RecyclerView
,我们需要添加recycleler视图依赖项。打开build.gradle(Module:app)文件,并com.android.support:recyclerview-v7:26.1.0
在dependencies部分添加以下“implementation' '”。单击立即同步以同步更改并构建项目。 - 在布局文件夹中打开activity_main.xml并添加Recycler View小部件。更改后,XML文件应如下所示:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.simplerecyclerviewexample.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rvFootballTeamInfo" android:scrollbars="vertical" ></android.support.v7.widget.RecyclerView> </android.support.constraint.ConstraintLayout>
对于Recycler
视图,我们提供了一个id并指定了滚动条是垂直的。
创建模型类和布局文件
- 我们将在
Recycler
视图中显示足球队相关信息。我们将创建一个用于存储信息的模型类。创建一个类FootballTeam.java和创建变量Name
,League
和YearEstablished
。隐藏 收缩 复制代码package com.example.simplerecyclerviewexample;public class FootballTeam {private String name;private String league; private int yearEstablished; public FootballTeam() { } public FootballTeam(String name, String league, int yearEstablished) { this.name = name; this.league = league; this.yearEstablished = yearEstablished; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLeague() { return league; } public void setLeague(String league) { this.league = league; } public int getYearEstablished() { return yearEstablished; } public void setYearEstablished(int yearEstablished) { this.yearEstablished = yearEstablished; } }
- 创建一个名为row.xml的布局文件。此布局将用于在回收站视图中显示每条记录。我们将创建一个
RelativeLayout
包含三个文本视图的简单视图,用于显示已建立的名称,联盟和年份。布局的完整XML如下所示:隐藏 收缩 复制代码<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" > <TextView android:id="@+id/tvName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" tools:text="Team Name" android:textSize="20dp" android:textStyle="bold" /> <TextView android:id="@+id/tvLeague" android:layout_width="match_parent" android:layout_height="wrap_content" tools:text="Team League"
转载于:https://www.cnblogs.com/langda/p/10453880.html
Android开发中如何使用RecyclerView相关推荐
- 史上最全Android开发中100%会用到的开源框架整理(1/5)
其实这个开源框架整理很久了,只是一直放在有道云笔记里面,笔者还有很多写得文章都放在有道云笔记里面,有时间都好好整理一下放出来,本篇文章也会不定期更新,由于整理的开源框架分类都有200多个,所有这次只将 ...
- android开发模式,Android开发中无处不在的设计模式
Android开发中无处不在的设计模式――单例模式 Android开发中无处不在的设计模式――Builder模式 前面介绍了单例模式和Builder模式,有兴趣的见上面两个链接,这篇文章侧重介绍1下视 ...
- Android开发中常见的设计模式
对于开发人员来说,设计模式有时候就是一道坎,但是设计模式又非常有用,过了这道坎,它可以让你水平提高一个档次.而在android开发中,必要的了解一些设计模式又是非常有必要的.对于想系统的学习设计模式的 ...
- android中资源文件的两种访问方式,在android开发中进行数据存储与访问的多种方式介绍...
在android开发中进行数据存储与访问的多种方式介绍 更新时间:2013年06月07日 16:24:23 作者: 很多时候我们的软件需要对处理后的数据进行存储或再次访问,Android为数据存储 ...
- android 最新 九宫格,Android开发中怎么显示一个九宫格图片
Android开发中怎么显示一个九宫格图片 发布时间:2020-11-21 15:09:33 来源:亿速云 阅读:86 作者:Leah 本篇文章给大家分享的是有关Android开发中怎么显示一个九宫格 ...
- Android开发中的日常积累
欢迎Star,Fork https://github.com/lizhangqu/CoreLink 里面记录了开发过程中有用的东西,欢迎补充,不定时更新. Android 性能优化 Android内存 ...
- Android 开发中的日常积累
Android 性能优化 Android性能优化视频,文档以及工具 胡凯-性能优化 Android最佳性能实践(1):合理管理内存 Android最佳性能实践(2):分析内存的使用情况 Android ...
- Android 开发中的观察者模式应用实例
前言 最近在遇到了 Android 的开发中常用到的设计模式之观察者模式,观察者模式,所谓的模式就是一种设计思想,可以按照某种模式,写出更合理,简单,有效的代码.可以用在 Android 开发中,也可 ...
- Android开发中的一些小知识点记录(101-120)
编写不易,如有转载,请声明出处:http://blog.csdn.net/zxc514257857/article/details/118554522 Android开发中的一些小知识点记录(1-20 ...
最新文章
- 讀書收穫 The Untold Story of Milk, Ron Schmid
- X265-线程池-1
- MySQL高级 trace工具
- 李幸原:看好实时音视频在教育与医疗的前景
- 痞子衡嵌入式:第一本Git命令教程(0)- 索引
- 与计算机相关的社团活动,计算机社团活动记录.doc
- react学习(7)----react转换值同render
- PHP全栈学习笔记27
- ZOJ3805Machine(二叉树左右子树变换)
- docker仓库搭建、加密、用户认证
- Vue开发总结 及 一些最佳实践 (已更新)
- 物联网监测之光纤振动传感器
- acu风格是什么意思_acu 是什么意思,是一个品牌吗???
- 偏微分 python_基于Python求解偏微分方程的有限差分法.doc
- 简单的实现app界面劫持和后台监控
- Lustre—配置和管理磁盘配额
- Ajax基础教程电子版
- 罗生门:动荡时代下人性的恶与善
- 先进先出SQL Server 语句
- 上海海洋大学计算机考研资料汇总
热门文章
- jquery下拉分页
- 29_Java_数据库_第29天(JDBC、DBUtils)_讲义
- Selenium VS QTP
- Mac和 iOS 下的对称和非对称加密算法的使用
- Cracking The Coding Interview5.2
- 邮件系统常见错误代码解释 --邮件服务器端(MTA)错误代码解释
- 动物为什么会预知地震,地震后为什么会下雨?
- [C# 基础知识系列]专题五:当点击按钮时触发Click事件背后发生的事情
- 02dayC语言数据类型
- VBA Editor Addins -- VBE插件模板开发众筹