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博客

相关推荐
m0_74823364几秒前
RabbitMQ 进阶
android·前端·后端
Mr.pyZhang4 小时前
Android构建系统 - 04 编译产物
android·linux
守城小轩10 小时前
Brave 132 编译指南 Android 篇 - 编译准备:系统要求与推荐工具 (三)
android·chrome·chrome devtools·指纹浏览器·浏览器开发·brave
m0_7482384210 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
程序员江同学11 小时前
Kotlin 技术月报 | 2025 年 2 月
android·kotlin
甜于酸12 小时前
MuMu模拟器Pro for Mac 安卓手机平板模拟器
android·macos·智能手机
顾林海15 小时前
深入解析 Android Activity 生命周期
android
XuanRanDev17 小时前
【构建工具】Gradle Kotlin DSL中的大小写陷阱:BuildConfigField
android·开发语言·kotlin
Nathan2024061619 小时前
Room - 基本使用及使用问题
android·面试·android jetpack
攻城狮_Dream21 小时前
基于 Python 的项目管理系统开发
android·数据库·python