Android RecyclerView的LayoutManager配置

devtools/2024/9/19 18:47:11/ 标签: android

RecyclerView的item布局方式依赖于其配置的布局管理器。不同的布局管理器可以实现不同的界面效果。

LayoutManager介绍

RecyclerView可以通过setLayoutManager设置布局管理器,该方法的源码如下:

    /*** Set the {@link LayoutManager} that this RecyclerView will use.** <p>In contrast to other adapter-backed views such as {@link android.widget.ListView}* or {@link android.widget.GridView}, RecyclerView allows client code to provide custom* layout arrangements for child views. These arrangements are controlled by the* {@link LayoutManager}. A LayoutManager must be provided for RecyclerView to function.</p>** <p>Several default strategies are provided for common uses such as lists and grids.</p>** @param layout LayoutManager to use*/public void setLayoutManager(@Nullable LayoutManager layout)

上述方法的入参为LayoutManager,该类为RecyclerView的抽象静态类。该类型的具体实现存在以下几种:
在这里插入图片描述

  • MaterialCalendar中的匿名实现类:日历管理器中实现的布局管理器,由于是内部匿名类,因此开发过程中无法进行使用。
  • LinearLayoutManager:线性布局管理器,线性排列RecyclerView的item,可以分为水平和垂直两个方向。
  • GridLayoutManager:网格布局管理器,继承了LinearLayoutManager,可以将item以网格的形式进行展示,构造方法中存在spanCount参数,可以表明每行存在数据的个数。
  • LinearLayoutManagerImpl:线性布局的进一步加工实现,主要为了viewPage的布局切换实现。
  • SmoothCalendarLayoutManager:com.google.android.material.datepicker包下package作用范围的 LinearLayoutManager子类。主要为日期选择器提供布局支持,开发过程中无法进行使用。
  • StaggeredGridLayoutManager:交错网格布局管理器,可以再两个方向(垂直、水平)交错的进行item布局,计算机输入键盘可以使用该布局进行实现(也可以用网格布局管理器)

LinearLayoutManager详解

/*** A {@link RecyclerView.LayoutManager} implementation which provides* similar functionality to {@link android.widget.ListView}.*/
public class LinearLayoutManager extends RecyclerView.LayoutManager implementsItemTouchHelper.ViewDropHandler, RecyclerView.SmoothScroller.ScrollVectorProvider 

ViewDropHandler:滑动消除、拖动支持的辅助接口
ScrollVectorProvider:平滑滚动的接口

最简单的构造方法,本质上是调用LinearLayoutManager(Context context, @RecyclerView.Orientation int orientation, boolean reverseLayout) 进行初始化实例。

    /*** Creates a vertical LinearLayoutManager** @param context Current context, will be used to access resources.*/public LinearLayoutManager(Context context) {this(context, RecyclerView.DEFAULT_ORIENTATION, false);}

参数较为齐全的构造方法,包含了布局方式【水平、竖直】以及是否反转item展示。
例如展示list=[1,2,3,4,5],如果reverseLayout=true则从左到右或从上到下(受orientation参数影响)的展示1,2,3,4,5,否则展示内容为:5,4,3,2,1

    /*** @param context       Current context, will be used to access resources.* @param orientation   Layout orientation. Should be {@link #HORIZONTAL} or {@link*                      #VERTICAL}.* @param reverseLayout When set to true, layouts from end to start.*/public LinearLayoutManager(Context context, @RecyclerView.Orientation int orientation,boolean reverseLayout) {setOrientation(orientation);setReverseLayout(reverseLayout);}

如注释说明,该构造方法使用通过XML配置线性构造管理器,相比较上面构造方法外该方法增加了setStackFromEnd(properties.stackFromEnd);调用。setStackFromEnd表明从底部进行数据加载和展示。

    /*** Constructor used when layout manager is set in XML by RecyclerView attribute* "layoutManager". Defaults to vertical orientation.** {@link android.R.attr#orientation}* {@link androidx.recyclerview.R.attr#reverseLayout}* {@link androidx.recyclerview.R.attr#stackFromEnd}*/public LinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,int defStyleRes) {Properties properties = getProperties(context, attrs, defStyleAttr, defStyleRes);setOrientation(properties.orientation);setReverseLayout(properties.reverseLayout);setStackFromEnd(properties.stackFromEnd);}

通过上述构造方法可以初始化创建一个线性布局管理器,并针对该管理器进行了基础配置。每个item都交友该管理器进行布局管理,此外线性管理器还提供配置布局参数(宽度、高度、权重)的方法,以及定位到指定位置的方法。

GridLayoutManager详解

网格管理器的构造方法类似于线性布局管理器,在其基础上还增加了spanCount参数的配置。该参数配置了在一行或者一列中展示item的最大数量

    /*** @param context Current context, will be used to access resources.* @param spanCount The number of columns or rows in the grid* @param orientation Layout orientation. Should be {@link #HORIZONTAL} or {@link*                      #VERTICAL}.* @param reverseLayout When set to true, layouts from end to start.*/public GridLayoutManager(Context context, int spanCount,@RecyclerView.Orientation int orientation, boolean reverseLayout) {super(context, orientation, reverseLayout);setSpanCount(spanCount);}

StaggeredGridLayoutManager详解

交错网格布局类似于网格布局的构造方法

使用示例

    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding = ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());recyclerView = binding.recyclerView;//设置垂直的线性布局管理器,Orientation --> VERTICAL:垂直 HORIZONTAL:水平LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false);layoutManager.setStackFromEnd(false);
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),2);StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,LinearLayoutManager.HORIZONTAL);recyclerView.setLayoutManager(staggeredGridLayoutManager);BaseAdapter<String> baseAdapter = new BaseAdapter<String>(R.layout.item_line) {@Overridepublic void bindView(ViewHolder holder, String obj, int position) {holder.setText(R.id.item,obj);}};baseAdapter.add("一个");baseAdapter.add("二个");baseAdapter.add("三个");baseAdapter.add("四个");baseAdapter.add("五个");baseAdapter.add("六个");baseAdapter.add("七个");recyclerView.setAdapter(baseAdapter);}

交错布局
在这里插入图片描述


http://www.ppmy.cn/devtools/2337.html

相关文章

Linux Zookeeper 安装

1 何为伪集群模式 在学习环境中&#xff0c;如果没有多余的服务器&#xff0c;这里就将三个ZooKeeper 节点都安装到本地机器上&#xff0c;故称谓伪集群模式。 虽然&#xff0c;伪集群模式只是便于开发、普通测试&#xff0c;尽量不用于生产环境。从学习的角度来说&#xff0c…

ChatGPT实用技巧:提升论文写作效果

ChatGPT无限次数:点击直达 ChatGPT实用技巧&#xff1a;提升论文写作效果 随着人工智能技术的不断发展&#xff0c;ChatGPT作为一种先进的自然语言生成模型&#xff0c;为学术界和科研人员提供了强大的写作辅助工具。本文将介绍如何利用ChatGPT提升论文写作效果&#xff0c;并…

Go Web框架-Beego

本文主要分享GO语言常用的web框架&#xff1a;Beego框架&#xff0c;简单分享如何快速入门Beego Beego框架 Beego框架的简介 Beego框架是一款开源的由国人开发的全栈式的Web框架&#xff0c;它采用了MVC架构&#xff0c;支持自动化路由、ORM、Session、日志、缓存等功能&#x…

中霖教育:二建考试中六个专业分别有什么特点?

建筑实务 《建筑实务》技术部分多以选择题为主&#xff0c;主要是对各种数据的考查;管理部分以案例题为主&#xff0c;旨在考查大家的综合能力&#xff0c;也是分值占比比较多的部分。进度控制的网络图和流水施工每年必考其一;质量管理主要结合技术部分命题;安全管理和合同管理…

python中中英文打印对齐解决方案

在python中&#xff0c;有时候会出现中英文混合输出的情形&#xff0c;但是由于中文默认是全角格式&#xff08;一个中文字符占用两个字符宽度&#xff09;&#xff0c;这会对python原生的print函数带来一些障碍。尤其是用户用print对齐输出的时候&#xff0c;这种差异会导致文…

HBase2.x学习笔记

文章目录 一、HBase 简介1、HBase 定义1.1 概述1.2 HBase 与 Hadoop 的关系1.3 RDBMS 与 HBase 的对比1.4 HBase 特征简要 2、HBase 数据模型2.1 HBase 逻辑结构2.2 HBase 物理存储结构2.3 HBase的表数据模型 3、HBase 基本架构3.1 Master3.2 Region Server3.3 Zookeeper3.4 HD…

第十四届蓝桥杯ABD题

A、阶乘求和&#xff1a; 【问题描述】 令 S 1! 2! 3! ... 202320232023! &#xff0c;求 S 的末尾 9 位数字。 提示&#xff1a;答案首位不为 0 。 【答案提交】 这是一道结果填空的题&#xff0c;你只需要算出结果后提交即可。本题的结果为一 个整数&#xff0c;在…

【QT进阶】Qt Web混合编程之CEF、QCefView简单介绍

往期回顾 【QT入门】Qt自定义控件与样式设计之自定义QLineEdit实现搜索编辑框-CSDN博客 【QT入门】Qt自定义控件与样式设计之自定义QTabWidget实现tab在左&#xff0c;文本水平的效果-CSDN博客 【QT进阶】Qt Web混合编程之CEF、QCefView简单介绍 一、web组件 Web组件是一种用…

webpack--区分开发环境和生产环境

区分开发和生产环境 初步使用 可以直接配置两个文件&#xff1a; dev const path require("path"); const { VueLoaderPlugin } require("vue-loader"); const HtmlWebpackPlugin require("html-webpack-plugin"); const { DefinePlugin…

Python 学习笔记(九)—— 操作系统和环境

目录 一、os模板 二、platform模块 三、扩展第三方库psutil 四、操作系统信息 4.1 使用platform模块 4.2 使用sys模块 4.3 使用os模块 4.4 使用subprocess模块 Python操作系统和环境主要指的是使用Python进行系统级操作和管理的相关功能和工具。 Python提供了许多用于…

ubuntu下的串口调试工具cutecom

系统&#xff1a;ubuntu20.04 &#xff08;1&#xff09;接线 使用 rs485&#xff1c;-----> rs232 转接口&#xff08; 设备直接出来的是rs485&#xff09;&#xff0c;电脑主机接入一根 rs232&#xff1c;-----> USB口 连接线&#xff0c;ubuntu系统下打开 termin…

博客系统项目测试(selenium+Junit5)

在做完博客系统项目之后&#xff0c;需要对项目的功能、接口进行测试&#xff0c;利用测试的工具&#xff1a;selenium以及Java的单元测试工具Junit进行测试&#xff0c;下面式测试的思维导图&#xff0c;列出该项目需要测试的所有测试用例&#xff1a; 测试结果&#xff08;全…

五个大学生必备的学习工具

五个大学生必备的学习辅助工具&#xff0c;做笔记、画思维导图、翻译、转格式、AI写作通通都能帮你解决&#xff01; 1、学霸必备思维导图–迅捷画图 直达链接>>> https://www.liuchengtu.com/ 可以下载软件&#xff0c;也可以直接收藏网址直接使用网页版&#xff0…

Android开发——ListView

activity_main.xml <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_height"match_parent"android:layout_width"match_parent"…

前端用 HTML5 + CSS3 + JavaScript,后端连接什么数据库更简单?

当前端使用 HTML5、CSS3 和 JavaScript 进行开发时&#xff0c;后端连接何种数据库是一个非常重要的问题&#xff0c;因为数据库的选择直接影响着后端代码的编写、数据存储与查询的效率以及系统的可维护性。 1. 关系型数据库&#xff08;SQL 数据库&#xff09;&#xff1a; …

如何快速打开Github

为什么我们打开Github速度很慢&#xff1f;很卡&#xff0c;甚至于访问不了&#xff0c;原因是中间有个域名通过DNS解析的过程&#xff0c;将域名解析为对应的ip地址&#xff0c;主要时间都是花在了DNS解析上了。 我们在浏览器输入 GitHub 的网址时&#xff0c;会向 DNS 服务器…

外贸企业邮箱有什么用?如何选择适合的外贸企业邮箱?

外贸公司每天都需要与各个国家的客户打交道&#xff0c;通过邮箱聊天、谈合作。由于语言、文化差异&#xff0c;一个小错误可能会致使业务失败和数据泄漏风险。做为外贸企业的重要沟通工具&#xff0c;企业电子邮件的功效是显而易见的。那样&#xff0c;外贸企业邮箱有什么用&a…

flutter知识点---生命周期

Flutter 应用的生命周期涉及两个层面&#xff1a;Widget&#xff08;组件&#xff09;的生命周期 和 应用程序&#xff08;App&#xff09;的生命周期。下面分别对这两个方面进行详细介绍&#xff1a; Widget&#xff08;组件&#xff09;的生命周期 Flutter 中的 Widget 是构…

【爬虫开发】爬虫从0到1全知识md笔记第5篇:Selenium课程概要,selenium的其它使用方法【附代码文档】

爬虫开发从0到1全知识教程完整教程&#xff08;附代码资料&#xff09;主要内容讲述&#xff1a;爬虫课程概要&#xff0c;爬虫基础爬虫概述,,http协议复习。requests模块&#xff0c;requests模块1. requests模块介绍,2. response响应对象,3. requests模块发送请求,4. request…

【电力工程】电力大数据和云架构智能AI服务平台研发建设项目可行性研究报告范例

1、项目概况 本项目拟进行基于电力大数据和云架构的智能 AI 服务平台的研究,具体包括电力多元大数据中心、技术中台、数据中台和智能 AI 中台,基于电力大数据云平台基础构建 BI 可视化开发平台和智能 AI 服务平台。 该项目的实施旨在引领公司在大数据领域发展的新趋势,从功…