移动技术开发:RecyclerView瀑布流水果列表

news2024/9/23 8:13:51

1 实验名称

       RecyclerView瀑布流水果列表

2 实验目的

       掌握RecyclerView控件的实现方法和基本应用

3 实验源代码

布局文件代码:

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

fruit_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
</LinearLayout>

Java代码:

MainActivity .java

public class MainActivity extends AppCompatActivity {

    private List<Fruit> fruitLists = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化水果列表数据
        initFruits();
        //创建循环列表对象
        RecyclerView recycler_view = findViewById(R.id.recycler_view);
        //创建一个线性布局管理器对象
        //LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //设置线性布局管理器的管理方向为水平管理
        //linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //设置循环列表的布局管理器
        //创建一个3列垂直的瀑布流布局管理器对象
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(staggeredGridLayoutManager);
        //创建列表数据适配器对象并把数据源关联上
        FruitAdapter fruitAdapter = new FruitAdapter(fruitLists);
        //让循环列表使用适配器对象
        recycler_view.setAdapter(fruitAdapter);
    }

    private void initFruits(){
        for (int i = 0; i < 2; i++) {
            Fruit apple = new Fruit(getRandomLengthName("Apple"), R.drawable.apple_pic);
            fruitLists.add(apple);
            Fruit banana = new Fruit(getRandomLengthName("Banana"), R.drawable.banana_pic);
            fruitLists.add(banana);
            Fruit orange = new Fruit(getRandomLengthName("Orange"), R.drawable.orange_pic);
            fruitLists.add(orange);
            Fruit watermelon = new Fruit(getRandomLengthName("Watermelon"), R.drawable.watermelon_pic);
            fruitLists.add(watermelon);
            Fruit pear = new Fruit(getRandomLengthName("Pear"), R.drawable.pear_pic);
            fruitLists.add(pear);
            Fruit grape = new Fruit(getRandomLengthName("Grape"), R.drawable.grape_pic);
            fruitLists.add(grape);
            Fruit pineapple = new Fruit(getRandomLengthName("Pineapple"), R.drawable.pineapple_pic);
            fruitLists.add(pineapple);
            Fruit strawberry = new Fruit(getRandomLengthName("Strawberry"), R.drawable.strawberry_pic);
            fruitLists.add(strawberry);
            Fruit cherry = new Fruit(getRandomLengthName("Cherry"), R.drawable.cherry_pic);
            fruitLists.add(cherry);
            Fruit mango = new Fruit(getRandomLengthName("Mango"), R.drawable.mango_pic);
            fruitLists.add(mango);
        }
    }

    private String getRandomLengthName(String name){
        Random random = new Random();
        int length = random.nextInt(20);
        StringBuilder stringBuilder = new StringBuilder();
        for(int i=0;i<length;i++){
            stringBuilder.append(name);
        }
        return stringBuilder.toString();
    }
}

FruitAdapter.java:

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
    private List<Fruit> fruitLists = null;//子项水果数据源列表

    public FruitAdapter(List<Fruit> fruitLists) {
        this.fruitLists = fruitLists;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //得到子项布局的视图
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fruit_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Fruit fruit = fruitLists.get(position);//根据位置得到一个水果对象
        //将水果图片放到子项视图持有器的ImageView中
        holder.fruit_image.setImageResource(fruit.getImageId());
        //将水果名字放到子项视图持有器的TextView中
        holder.fruit_name.setText(fruit.getName());
    }

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

    //列表子项的视图持有器
    static class ViewHolder extends RecyclerView.ViewHolder{

        ImageView fruit_image;
        TextView fruit_name;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            fruit_image = itemView.findViewById(R.id.fruit_image);
            fruit_name = itemView.findViewById(R.id.fruit_name);
        }
    }
}

Fruit.java:

//自定义数据源类
public class Fruit {

    private String name;
    private int imageId;//水果图片ID号

    public Fruit(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
}

4 实验运行结果图

5 实验总结

       先写布局文件,两个布局文件,分别是activity_main.xml布局文件和fruit_item.xml布局文件。在activity_main.xml布局文件中,设置了一个瀑布流列表;而fruit_item.xml布局文件中在列表中插入图片和文本框。

       写完布局文件开始写Java代码,初始化水果列表数据,创建循环列表对象,创建一个三列垂直的瀑布流布局管理器对象,创建列表数据适配器对象并把数据源关联上,让循环列表适用适配器对象,将水果图片和名字放到子项视图持有器中。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2157053.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【学习笔记】手写 Tomcat 五

目录 一、优化 Servlet 创建一个抽象类 继承抽象类 二、三层架构 业务逻辑层 数据访问层 1. 在 Dao 层操作数据库 2. 调用 Dao 层&#xff0c;实现业务逻辑功能 3. 调用 Service 层&#xff0c;响应数据 测试 三、数据库连接池 1. 手写数据库连接池 2. 创建数据库…

C语言题目之单身狗2

文章目录 一、题目二、思路三、代码实现 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、题目 二、思路 第一步 在c语言题目之打印单身狗我们已经讲解了在一组数据中出现一个单身狗的情况&#xff0c;而本道题是出现两个单身狗的情况。根据一个数…

当Navicat报错 Can not connect to MySQL server的解决方法!

今天运行数据库时突然弹出一个error&#xff1a; 原因&#xff1a;MySQL的服务没有打开&#xff0c;需要检查MySQL的开启状态即可。 具体做法&#xff1a; 1.右键“开始”&#xff0c;点击“计算机管理” 2. 选择“服务和应用程序”&#xff0c;并点击“服务” 3.在服务中找…

ESP32-WROOM-32 [创建AP站点-TCP服务端-数据收发]

简介 ESP32 创建TCP Server AP站点&#xff0c; PC作为客户端连接站点并收发数据 指令介绍 注意,下面指令需要在最后加上CRLF, 也就是\r\n(回车换行) ATRESTORE // 恢复出厂设置 ATCWMODE2 // 设置 Wi-Fi 模式为 softAP ATCIPMODE0 // 需要数据传输模式改为0&#xff0c; 普通…

Cesium 绘制可编辑点

Cesium Point点 实现可编辑的pointEntity 实体 文章目录 Cesium Point点前言一、使用步骤二、使用方法二、具体实现1. 开始绘制2.绘制事件监听 三、 完整代码 前言 支持 鼠标按下 拖动修改点&#xff0c;释放修改完成。 一、使用步骤 1、点击 按钮 开始 绘制&#xff0c;单…

河钢数字PMO牛红卫受邀为第四届中国项目经理大会演讲嘉宾

全国项目经理专业人士年度盛会 河钢数字技术股份有限公司项目管理部PMO牛红卫受邀为PMO评论主办的全国项目经理专业人士年度盛会——2024第四届中国项目经理大会演讲嘉宾&#xff0c;演讲议题为“从技术到领导力——项目经理成长进阶之道”。大会将于10月26-27日在北京举办&…

知情人称,丹尼尔克雷格在卸任“007”以后他和蕾切尔薇兹的婚姻“产生了奇效”

丹尼尔克雷格、蕾切尔薇兹 虽然丹尼尔克雷格 (Daniel Craig) 因出演詹姆斯邦德 (James Bond) 而成为全球最耀眼的明星之一&#xff0c;实现了自己以及很多人的梦想&#xff0c;但知情人称他与蕾切尔薇兹 (Rachel Weisz) 的婚姻实际上正因此而陷入困境&#xff1b;但现在&#…

C# winforms DataGridView设置数据源自动显示表格

初级代码游戏的专栏介绍与文章目录-CSDN博客 我的github&#xff1a;codetoys&#xff0c;所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。 这些代码大部分以Linux为目标但部分代码是纯C的&#xff0c;可以在任何平台上使用。 源码指引&#xff1a;github源…

高效打造知识图谱,使用LlamaIndex Relik实现实体关联和关系抽取

大家好&#xff0c;文本信息转化为知识图谱的技术&#xff0c;自问世以来一直是研究界的宠儿。大型语言模型&#xff08;LLMs&#xff09;的兴起让这个领域受到更多关注&#xff0c;但LLMs的成本之高令人却步。然而通过对小型模型微调优化&#xff0c;可以找到一种更经济高效的…

没有 Microsoft Wi-Fi Direct Virtual Adapter #2 导致无法打开热点

我的环境 电脑打不开热点 系统 win11 64位 品牌 hp 笔记本电脑 解决方法&#xff1a; https://answers.microsoft.com/zh-hans/windows/forum/all/%E7%A7%BB%E5%8A%A8%E7%83%AD%E7%82%B9%E6%97%A0/9285620a-71d9-4671-b125-4cd607b6371a 解决 &#x1f613; 扫描一下设…

读构建可扩展分布式系统:方法与实践12分布式数据库案例

1. Redis 1.1. 2009年首次发布 1.1.1. 更注重原始性能和简单性&#xff0c;而不是数据安全性和一致性 1.2. 主要吸引力在于它能够同时充当分布式缓存和数据存储 1.3. 维护一个内存中的数据存储&#xff0c;也称为数据结构存储(data structure store) 1.4. 配置Redis将每个…

每日学习一个数据结构-Trie树(字典树)

文章目录 定义节点结构根节点插入操作查找操作删除操作特点应用示例 “Trie”树&#xff0c;又称为前缀树或字典树&#xff0c;是一种专门用于存储字符串的数据结构。它在许多应用程序中都非常有用&#xff0c;特别是在那些需要高效查找、插入和删除字符串的应用场景中。下面是…

2024年华为杯数学建模E题-高速公路应急车道启用建模-基于YOLO8的数据处理代码参考(无偿分享)

利用YOLO模型进行高速公路交通流量分析 识别效果&#xff1a; 免责声明 本文所提供的信息和内容仅供参考。尽管我尽力确保所提供信息的准确性和可靠性&#xff0c;但我们不对其完整性、准确性或及时性作出任何保证。使用本文信息所造成的任何直接或间接损失&#xff0c;本人…

记一次Meilisearch轻量级搜索引擎使用

以前使用的是mysql的全文索引、最开始还行。后续觉得就不好用了&#xff0c;但是服务器资源有限&#xff0c;没法上ES&#xff0c;只好找一个轻量级的搜索引擎、找了半天&#xff0c;决定使用这一个&#xff0c;目前效果还不错的。 参考网址 官网&#xff1a;https://www.meil…

java反射基础知识

1.java的反射机制 Java 反射机制是在运行状态中&#xff0c;对于任意一个类&#xff0c;都能够知道这个类的所有属性和方法&#xff1b;对于任意一个对象&#xff0c;都能够调用它的任意方法和属性&#xff1b;这种动态获取信息以及动态调用对象方法的功能称为 Java 语言的反射…

【C语言零基础入门篇 - 16】:栈和队列

文章目录 栈和队列栈栈功能的实现源代码 队列队列功能的实现源代码 栈和队列 栈 什么是栈&#xff1a;功能受限的线性数据结构 栈的特点&#xff1a;先进后出 。例如&#xff1a;仓库进货、出货。 栈只有一个开口&#xff0c;先进去的数据在栈底&#xff08;bottom&#xf…

高版本idea开启热部署找不到compiler.automake.allow.when.app.running

高版本idea开启热部署找不到compiler.automake.allow.when.app.running 1.解决方案 设置----》高级设置 搜索 allow 勾选完成就能使用了

针对论坛系统设计测试用例

针对用户登录设计测试用例 针对站内信编写测试用例

mysql5.7小版本升级

最近因为mysql漏洞问题需要升级版本 目标&#xff1a;5.7.17升级到最新的5.7.44 提前准备&#xff1a;必须 MySQL :: Download MySQL Community Server (Archived Versions) 下载最新的5.7版本5.7.44&#xff0c;下载后&#xff0c;解压备用 ​ 方案1&#xff1a;卸载原有…

网站建设模板选择哪种

在选择网站建设模板时&#xff0c;需要考虑多个因素&#xff0c;包括网站的目的、受众、内容类型以及个性化需求等。以下是一些常见的网站建设模板类型&#xff0c;以及它们的特点&#xff0c;希望对你的选择有所帮助。 企业/商务模板&#xff1a; 企业和商务网站通常需要专业、…