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

相关推荐
ii_best42 分钟前
按键精灵支持安卓14、15系统,兼容64位环境开发辅助工具
android
美狐美颜sdk1 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭6 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
aqi006 小时前
FFmpeg开发笔记(七十七)Android的开源音视频剪辑框架RxFFmpeg
android·ffmpeg·音视频·流媒体
androidwork8 小时前
深入解析内存抖动:定位与修复实战(Kotlin版)
android·kotlin
梦天20158 小时前
android核心技术摘要
android
szhangbiao10 小时前
“开发板”类APP如果做屏幕适配
android
高林雨露11 小时前
RecyclerView中跳转到最后一条item并确保它在可视区域内显示
android
移动开发者1号13 小时前
ReLinker优化So库加载指南
android·kotlin
山野万里__13 小时前
C++与Java内存共享技术:跨平台与跨语言实现指南
android·java·c++·笔记