2024-08-16升级记录:使用Android RecyclerView控件显示列表型信息

在页面上使用RecyclerView实现一个列表型信息展示:

步骤如下:

一、在页面布局中添加RecyclerView控件

XML 复制代码
            <TextView
                android:id="@+id/txt_gnss_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="卫星信息"
                android:textColor="#0a0a0a" />
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_gnss_country"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                />

二、添加单行显示的item布局文件

layout_gnss_counrty_item.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/iv_item_flag"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dp"
        android:src="@drawable/flag_beidou_3"
        android:layout_centerVertical="true"
        />

    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GNSS国家名称"
        android:layout_centerVertical="true"
        android:textColor="@color/layer_name"
        android:layout_marginLeft="8dp"
        android:layout_gravity="center_vertical"/>

</LinearLayout>

三、逻辑实现代码

实体类:

java 复制代码
public class GnssCountryInfo {
    public int FlagId;
    public String TextContext;

    public GnssCountryInfo(int _id,String _str){
        FlagId = _id;
        TextContext = _str;
    }
}

Adapter类和ViewHolder类:

java 复制代码
public class GnssCountryAdapter extends RecyclerView.Adapter<GnssCountryAdapter.GnssCountryViewHoder> {

    private Context context;
    private List<GnssCountryInfo> dataList;

    public GnssCountryAdapter(Context _context, List<GnssCountryInfo> _data){
        this.dataList = _data;
        this.context = _context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull GnssCountryViewHoder holder, int position) {
        GnssCountryInfo itemData = dataList.get(position);
        holder.mCountryFlag.setImageResource(itemData.FlagId);
        holder.mTitleContent.setText(itemData.TextContext);
    }

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


    class GnssCountryViewHoder extends RecyclerView.ViewHolder {
        ImageView mCountryFlag;
        TextView mTitleContent;

        public GnssCountryViewHoder(@NonNull View itemView) {
            super(itemView);
            mCountryFlag = itemView.findViewById(R.id.iv_item_flag);
            mTitleContent = itemView.findViewById(R.id.tv_item_name);
        }
    }

}

调用:

java 复制代码
        GnssCountryAdapter adapter = new GnssCountryAdapter(this,dataList);
        mRecyclerView.setAdapter(adapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);

注意:

一定要设置:

LinearLayoutManager layoutManager = new LinearLayoutManager(this);

mRecyclerView.setLayoutManager(layoutManager);

否则会出错。

参考链接:

Android RecyclerView最全使用详解-CSDN博客

RecyclerView相关学习资料:

Android开发,使用RecyclerView实现商品列表_安卓recyclerview设备列表-CSDN博客

Android RecyclerView控件_recycleview版本-CSDN博客

RecyclerView之下拉刷新、下拉加载的实现_androidx.recyclerview.widget.recyclerview下拉刷新 底部加载-CSDN博客

相关推荐
沐怡旸20 小时前
【底层机制】【Android】深入理解UI体系与绘制机制
android·面试
啊森要自信20 小时前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash
下位子21 小时前
『AI 编程』用 Codex 开发识字小帮手应用
android·openai·ai编程
Zender Han21 小时前
Flutter 实现人脸检测 — 使用 google_mlkit_face_detection
android·flutter·ios
君逸臣劳21 小时前
玩Android Flutter版本,通过项目了解Flutter项目快速搭建开发
android·flutter
叫我龙翔1 天前
【MySQL】从零开始了解数据库开发 --- 基本查询
android·mysql·数据库开发
2501_916008891 天前
iOS 26 性能分析深度指南 包含帧率、渲染、资源瓶颈与 KeyMob 协助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
撩得Android一次心动1 天前
Android adb 基础使用指南
android·adb
为java加瓦1 天前
PHP MQTT 订阅服务:实时消息接收与数据库存储解决方案
android
怿星科技1 天前
Android MVVM架构解析:现代开发的首选模式
android·架构