RecyclerView使用示例(瀑布流)

简介: RecyclerView使用示例(瀑布流)

效果图

代码示例

ShopFragment.java(显示RecyclerView的Fragment)

pacpackage com.gjc.ihelp.fragment;

import android.app.Fragment;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.gjc.ihelp.R;
import com.gjc.ihelp.adapter.GoodsAdapter;
import com.gjc.ihelp.data.GoodsData;

import java.util.ArrayList;
import java.util.List;

public class ShopFragment extends Fragment {

    private View view;
    private TextView tvTitle;
    private RecyclerView rvList;

    //页面数据
    private List<GoodsData> goodsDataList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_shop, container, false);
        initData();
        initView();
        return view;
    }

    private void initData() {
        goodsDataList = new ArrayList<>();

        // 初始化用户数据
        for (int i = 0; i < 100; i++) {
            GoodsData goodsData = new GoodsData();
            if (Math.random() * 100 < 10) {
                goodsData.goodsImg = R.drawable.goods;
            } else if (Math.random() * 100 >= 10 && Math.random() * 100 < 20) {
                goodsData.goodsImg = R.drawable.goods_1;
            } else if (Math.random() * 100 >= 20 && Math.random() * 100 < 30) {
                goodsData.goodsImg = R.drawable.goods_2;
            } else if (Math.random() * 100 >= 30 && Math.random() * 100 < 40) {
                goodsData.goodsImg = R.drawable.goods_3;
            } else if (Math.random() * 100 >= 40 && Math.random() * 100 < 50) {
                goodsData.goodsImg = R.drawable.goods_4;
            } else if (Math.random() * 100 >= 50 && Math.random() * 100 < 60) {
                goodsData.goodsImg = R.drawable.goods_5;
            } else if (Math.random() * 100 >= 60 && Math.random() * 100 < 70) {
                goodsData.goodsImg = R.drawable.goods_6;
            } else if (Math.random() * 100 >= 70 && Math.random() * 100 < 80) {
                goodsData.goodsImg = R.drawable.goods_7;
            } else if (Math.random() * 100 >= 80 && Math.random() * 100 < 90) {
                goodsData.goodsImg = R.drawable.goods_8;
            } else {
                goodsData.goodsImg = R.drawable.goods_9;
            }
            goodsData.integral = (int) (Math.random() * 100 + 100);
            goodsDataList.add(goodsData);
        }

    }

    private void initView() {
        tvTitle = view.findViewById(R.id.tv_title);
        rvList = view.findViewById(R.id.rv_list);

        tvTitle.setText("商品列表");
        /**
         * StaggeredGridLayoutManager 方法参数
         *    setSpanCount:设置网格的列数
         *    setOrientation:设置瀑布流布局的方向,取值说明同LinearLayoutManager
         *    setReverseLayout:设置是否为相反方向开始布局,默认false。如果设置为true,那么垂直方向将从下往上开始布局,水平方向将从右往左开始布局
         */
        // 2:设置网格列数 StaggeredGridLayoutManager.VERTICAL:设置瀑布流布局的方向
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        GoodsAdapter myAdapter = new GoodsAdapter(getActivity(), goodsDataList);
        //添加 adapter
        rvList.setAdapter(myAdapter);
        //添加 manager
        rvList.setLayoutManager(manager);

    }

}

fragment_shop.xml(相对应的xml文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".fragment.ShopFragment">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="@string/user_list"
        android:textSize="30sp"
        android:textStyle="bold"
        android:gravity="center"
        android:textColor="#ffffff"/>
    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_list"
        android:background="#f0f0f0"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

GoodsData.java(数据源)

package com.gjc.ihelp.data;

public class GoodsData {

    public int goodsImg;//商品图片
    public int integral;//所需积分

    public GoodsData() {
    }

}

GoodsAdapter.java(适配器类)

package com.sjm.ihelp.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sjm.ihelp.R;
import com.sjm.ihelp.Utils.ToastUtil;
import com.sjm.ihelp.data.GoodsData;

import java.util.List;

public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.MyViewHolder> {

    private Context context;

    private List<GoodsData> goodsDataList;
    private ImageView ivGoodsImg;
    private TextView tvNeedIntegral;
    private Button btExchange;

    public GoodsAdapter(Context context, List<GoodsData> goodsDataList) {
        this.context = context;
        this.goodsDataList = goodsDataList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.adapter_goods, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ivGoodsImg.setBackgroundResource(goodsDataList.get(position).goodsImg);
        tvNeedIntegral.setText(goodsDataList.get(position).integral + "");
        btExchange.setOnClickListener(v -> {
            ToastUtil.show((Activity) context, "兑换成功");
        });
    }

    @Override
    public int getItemCount() {
        return 50;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            //获取文本节点
            ivGoodsImg = itemView.findViewById(R.id.iv_goods_img);
            btExchange = itemView.findViewById(R.id.bt_exchange);
            tvNeedIntegral = itemView.findViewById(R.id.tv_need_integral);
        }
    }

}

adapter_goods.xml(item)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_goods_img"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/goods_2"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="horizontal">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="所需积分:"
                android:textSize="18sp"
                android:textColor="#000000"/>
            <TextView
                android:id="@+id/tv_need_integral"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="999"
                android:textSize="18sp"
                android:textColor="#ff0000"/>
        </LinearLayout>

        <Button
            android:id="@+id/bt_exchange"
            android:layout_width="70dp"
            android:layout_height="30dp"
            android:text="@string/exchange"
            android:layout_marginRight="10dp"
            android:layout_gravity="right|center"
            android:background="@drawable/button_style_orange"/>
    </FrameLayout>

</LinearLayout>
<?xml
目录
相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8076 2
|
11天前
|
域名解析 人工智能 Linux
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10234 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1537 5
|
9天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2693 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2345 0