Android --- RecycleView

2024-04-25 16:20
文章标签 android recycleview

本文主要是介绍Android --- RecycleView,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

RecyclerView是官方在5.0之后新添加的控件,推出用来替代传统的ListViewGridView列表控件。

使用 RecyclerView 步骤

  • 在布局文件中定义的一个 RecyclerView 实例,用作视图的容器。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MyRecycleView"><TextViewandroid:id="@+id/textView7"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="RecycleView"android:textAlignment="center"android:textColor="@color/design_default_color_primary_dark"android:textSize="20sp"app:layout_constraintEnd_toEndOf="@+id/recycleView"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recycleView"android:layout_width="match_parent"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/textView7"app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • 布局管理器

布局管理器负责处理视图中界面组件的组织(布局)

RecyclerView提供了三种布局管理器即:

  • LinearLayoutManager 线性布局管理器
  • StaggeredGridLayoutManager 瀑布流布局管理器
  • GridLayoutManager 网格布局管理器

都是通过setLayoutManager方法来设置

public class MyRecycleView extends AppCompatActivity {private RecyclerView recyclerView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_my_recycle_view);// 设置布局LinearLayout linearLayout = new LinearLayout(this);recyclerView = findViewById(R.id.recycleView);LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));}
}
  • 准备数据项item的布局

如果所有列表项看起来都一样,您可以对所有这些列表项使用相同的布局,但这不是强制性要求。项布局必须与 fragment 的布局分开创建,以便一次创建一个项视图,并在其中填充数据。

recycle_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 创建item布局-->
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="8dp"app:cardCornerRadius="20dp"app:cardElevation="3dp"><LinearLayoutandroid:id="@+id/linearLayout1111"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/black"android:orientation="vertical"><TextViewandroid:id="@+id/hourTxt"android:textColor="@color/white"android:textSize="14sp"android:textAlignment="center"android:layout_margin="8dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView" /><TextViewandroid:id="@+id/tempTxt"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="20"android:textSize="18sp"android:textColor="@color/white"android:textAlignment="center"/></LinearLayout>
</androidx.cardview.widget.CardView>
  • 准备显示的数据。

int[] colors = {R.color.p1, R.color.p2, R.color.p3, R.color.p4, R.color.p5, R.color.p6};
  • 适配器

该适配器可将您的数据与 RecyclerView 相关联。它会调整数据,使其可在 ViewHolder 中显示。RecyclerView 会使用适配器确定如何在屏幕上显示数据。

新建一个类继承 RecyclerView.Adapter<VH>的Adapter类(VH是ViewHolder的类名) ---- MyRecycleViewAdapter

创建ViewHolder:在Adapter中创建一个继承RecyclerView.ViewHolder的静态内部类,记为VH。ViewHolder的实现和ListView的ViewHolder实现几乎一样(为了减少每一个item布局找控件的时间)

在 Adapter中实现3个方法:

onCreateViewHolder() 为每个Item inflate出一个View,View直接封装在ViewHolder中,每一个item布局里的控件就在ViewHolder中找到 

onBindViewHolder() 这个方法主要用于适配渲染数据到View中->item View中

getItemCount() 获取item长度

package com.example.androidstudiostudy;import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;// 创建 RecyclerView适配器
/* 1. 新建一个类继承 RecyclerView.Adapter<VH>的Adapter类(VH是ViewHolder的类名) ---- MyRecycleViewAdapter* 2. 创建ViewHolder:在Adapter中创建一个继承RecyclerView.ViewHolder的静态内部类,记为VH。ViewHolder的实现和ListView的ViewHolder实现几乎一样(为了减少每一个item布局找控件的时间)* 3. 在 Adapter中实现3个方法:*    onCreateViewHolder() 为每个Item inflater出一个View,View直接封装在ViewHolder中,每一个item布局里的控件就在ViewHolder中找到*    onBindViewHolder() 这个方法主要用于适配渲染数据到View中->item View中*    getItemCount() 获取item长度* 可以看出,RecyclerView将ListView中getView()的功能拆分成了onCreateViewHolder()和onBindViewHolder()。*/public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder> {// 创建静态类 ViewHolder// 可以视为 onCreate 方法,在这里获取行布局中的所有控件public static class ViewHolder extends RecyclerView.ViewHolder{private Color color;private LinearLayout linearLayout;public ViewHolder(@NonNull View itemView) {super(itemView);linearLayout = itemView.findViewById(R.id.linearLayout1111);}}private final int[] colors;// 构造函数 获取数据public MyRecycleViewAdapter(int[] colors) {this.colors = colors;}@NonNull@Override//膨胀布局 为每一行提供外观的地方public MyRecycleViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_view_itemt,parent,false);return new ViewHolder(view);}@Override// 将每一行的数据值和每一行的布局进行绑定public void onBindViewHolder(@NonNull MyRecycleViewAdapter.ViewHolder holder,int position) {holder.linearLayout.setBackgroundColor(colors[position]);holder.itemView.setOnClickListener(v -> System.out.print("点击了颜色:"));}@Overridepublic int getItemCount() {return colors.length;}}

这篇关于Android --- RecycleView的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/935184

相关文章

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问