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

相关推荐
玲小珑14 分钟前
Auto.js 入门指南(七)定时任务调度
android·前端
墨狂之逸才1 小时前
adb常用命令调试
android
YoungForYou1 小时前
Android端部署NCNN
android
移动开发者1号1 小时前
Jetpack Compose瀑布流实现方案
android·kotlin
移动开发者1号1 小时前
Android LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout大混战
android·kotlin
移动开发者1号1 小时前
ListView与RecyclerView区别总结
android·kotlin
移动开发者1号1 小时前
OkHttp 3.0源码解析:从设计理念到核心实现
android·kotlin
小草帽学编程5 小时前
鸿蒙Next开发真机调试签名申请流程
android·华为·harmonyos
dog shit7 小时前
web第十次课后作业--Mybatis的增删改查
android·前端·mybatis
科技道人8 小时前
Android15 launcher3
android·launcher3·android15·hotseat