Android studio RecyclerView 应用设计

一、创建empty activity项目:

二、打开activity_main.xml布局文件:

添加RecyclerView控件

复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

三、新建子布局文件

四、修改子布局文件,添加item:

复制代码
<ImageView
    android:id="@+id/imageView"
    android:layout_width="32dp"
    android:layout_height="32dp"
    tools:srcCompat="@tools:sample/avatars" />

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="32dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="64dp"
    android:text="TextView" />

五、添加adapter:

六、MainActivity.java:

定义:

复制代码
private RecyclerView recyclerView;
private ProvinceAdapter provinceAdapter;
private List<Province> provinceList = new ArrayList<>();

初始化:

复制代码
recyclerView = findViewById(R.id.recyclerview);

String provinceName[] = {"北京","上海","天津","重庆",
                         "内蒙古","广西","宁夏","新疆","西藏",
                         "香港","澳门",
                         "河北","河南","山东","山西","辽宁",
                         "吉林","黑龙江","江苏","浙江","安徽",
                         "福建","江西","湖北","湖南","广东",
                         "海南","四川","贵州","云南","陕西",
                         "甘肃","青海","台湾"        };
String provincialCapital[] = {
        "北京","上海","天津","重庆",
        "呼和浩特","南宁","银川","乌鲁木齐","拉萨",
        "香港","澳门",
        "石家庄","郑州","济南","太原","沈阳",
        "长春","哈尔滨","南京","杭州","合肥",
        "福州","南昌","武汉","长沙","广州",
        "海口","成都","贵阳","昆明","西安",
        "兰州","西宁","台北"

};


for (int i = 0; i < 34; i++){
    Province province = new Province();
    province.name = provinceName[i];
    province.provincialCapital = provincialCapital[i];
    provinceList.add(province);
}

引用:

复制代码
provinceAdapter = new ProvinceAdapter();
recyclerView.setAdapter(provinceAdapter);
GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this,2);
recyclerView.setLayoutManager(gridLayoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL);
recyclerView.addItemDecoration(dividerItemDecoration);

七、运行效果:

相关推荐
三少爷的鞋8 小时前
为什么应该先在 IntelliJ 中学习 Kotlin 与协程,而不是直接上 Android Studio
android
不爱说话郭德纲1 天前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
Sinclair1 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
雮尘1 天前
手把手带你玩转Android gRPC:一篇搞定原理、配置与客户端开发
android·前端·grpc
ktl1 天前
Android 编译加速/优化 80%:一个文件搞定,零侵入零配置
android
alexhilton2 天前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
冬奇Lab2 天前
InputManagerService:输入事件分发与ANR机制
android·源码阅读
张小潇2 天前
AOSP15 Input专题InputManager源码分析
android·操作系统
RdoZam2 天前
Android-封装基类Activity\Fragment,从0到1记录
android·kotlin
奥陌陌2 天前
android 打印函数调用堆栈
android