当前位置:网站首页>2. Basic use RecyclerView
2. Basic use RecyclerView
2022-08-02 14:52:00 【Fall in love with learning!】
详解RecyclerView系列文章目录
概述
In the previous article, we briefly introduced what it isRecyclerView,and what advantages does he have.So from the beginning of this article, we will start to learn step by stepRecyclerView的使用方法.实现的效果图如下:
The first step in using a control is definitely to add dependencies,That is, import relatedjar或者是aar包,这里我们直接gradle配置,如果你使用的Eclipsee那我只能呵呵了.
添加依赖
In this step you just need to addRecyclerView的依赖配置,I only explain hereAndroid Studio中配置.
### Gradle
Add the following code to yoursmodule的build.gradle文件dependencies代码块中
compile 'com.android.support:design:25.2.0'
然后重新build,You can automatically reference the dependent library to your project.Then you can use the control.
添加RecyclerView到布局
Add the layout code below to yoursxml中:
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
After the controls are all added,Next, you need to configure it in the codeRecyclerView了.
配置RecyclerView
//如果你知道你的RecyclerViewThe height does not change during use
// Using this setting can improve performance,A later article will analyze why
rv.setHasFixedSize(true);
//设置一个布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
rv.setLayoutManager(linearLayoutManager);
Here we set a layout manager after finding the control.The so-called layout manager is to tellRecyclerViewHow to display eachItem,这也就是RecyclerViewOne of the spermatic cords of this control,How he will show eachItem,也就是每个Itemhow to place,Abstract to a callLayoutManager的类,You can customize different classes to display different effectsItem,从而实现类似ListView,GridViewand waterfalls, etc.The advantage of this is that the different layouts can be dynamically changed.
After completing the basic configuration,我们需要给他设置一个adapter,这和ListView的使用方法是一样的.
设置Adapter
我们先新建一个Adapter,然后继承RecyclerView.Adapter
class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private final LayoutInflater layoutInflater;
public MyAdapter(Context context) {
layoutInflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}
Next we need to implement the corresponding method,But before implementing the corresponding method, we still need to understand the use of each method:
onCreateViewHolder:This method only creates oneViewHolder的时候调用
onBindViewHolder:This method displays one at a timeItem时,调用他,将ViewHolder传递给你,Set the data you want to display here
getItemCount:就是Item的数量
You can see how to use it hereListView差不多,It's just that he will be where we used to begetViewThe events made in the method are divided into two steps,The first step is to createViewHolder,Another step is to bind the data to be displayed.This way the user does not need to judge as badly as beforeconvertView是否为空了.
Now that the user of each method is known,Next, you can implement each method,但是我们发现onCreateViewHolder返回的是ViewHolder,So I need one moreViewHolder类:
创建ViewHolder
class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv = (TextView)itemView. findViewById(android.R.id.text1);
}
public void bindData(String d) {
tv.setText(d);
}
}
We found it in the constructor classTextView,There are also some frameworks that can be used here to avoid writingfindViewById,Because we are still writing basic articles, so let's not make it so complicated,Subsequent articles in this series will take you to encapsulateAdapter,ViewHolder等.Here, let's make it simple to understand.
The next thing you need to do is implement itAdaptermethod inside.
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(
layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false));
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
String d = BasicUseActivity.this.data.get(position);
holder.bindData(d);
}
@Override
public int getItemCount() {
return data.size();
}
然后我们就可以在Activity中设置adapter了.
//设置适配器
MyAdapter myAdapter = new MyAdapter(this);
rv.setAdapter(myAdapter);
接下来就是见证奇迹的时候了,We can run the project to the phone.Check out your results.
边栏推荐
- redis delay queue
- Flask contexts, blueprints and Flask-RESTful
- 线性代数期末复习存档
- MySQL知识总结 (二) 存储引擎
- 统计偏科最严重的前100名学生
- C语言初级—常见问题(100~200素数,计算1+11+111+...,从键盘获取一个数并输出有几个位)
- Visual Studio配置OpenCV之后,提示:#include<opencv2/opencv.hpp>无法打开源文件
- 浏览器报错数字代表的大概意思
- verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十四章)
- [ROS] (02) Create & compile ROS package Package
猜你喜欢
Visual Studio配置OpenCV之后,提示:#include<opencv2/opencv.hpp>无法打开源文件
MongoDB Compass 安装与使用
MySQL知识总结 (六) MySQL调优
Using the cloud GPU + pycharm training model to realize automatic background run programs, save training results, the server automatically power off
c语言三子棋详解!!! (电脑智能下棋)(附上完整代码)
Redis持久化机制
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第九章)
UIWindow的makeKeyAndVisible不调用rootviewController 的viewDidLoad的问题
华为防火墙
PostgreSQL 性能谜题
随机推荐
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十一章)
原码、补码、反码
【c】小游戏---扫雷雏形
A little thought about password encryption
【c】大学生在校学习c语言常见代码
宝塔搭建DM企业建站系统源码实测
C语言字符串——关于指针
Hession使用
Scala学习总结
redis基础
MapReduce流程
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第九章)
MySQL知识总结 (十一) MySql 日志,数据备份,数据恢复
C语言日记 6 基本输入/输出
标签加id 和 加号 两个文本框 和一个var 赋值
Spark_DSL
Flask framework in-depth two
宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
重新学习编程day1 【初始c语言】【c语言编写出计算两个数之和的代码】
C语言sizeof和strlen的区别