Day 14:RecyclerView 进阶项目布局

本篇文章同步发表在 HKT 线上教室 部落格,线上影音教学课程已上架至 UdemyYoutube 频道。另外,想追踪更多相关技术资讯,欢迎到 脸书粉丝专页 按赞追踪喔~

程序码范例

范例名称:RecyclerView 进阶项目布局
开发人员:HKT (侯光灿)
程序语言:Kotlin
开发环境:Android Studio 4.1.2 & Android 11 & Kotlin 1.4.21
授权范围:使用时必须注明出处且不得为商业目的之使用
范例下载点:点我下载

昨天,我们使用了一个 TextView,显示药局名称,这次我们想要让项目,显示更多口罩重点资讯,所以我们将进行调整项目布局。

滚动列表项目 Wireframe

置中显示药局名称与大人、小孩口罩数量。

项目布局

修改 layout/item_view.xml。

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:paddingBottom="20dp"
    android:focusable="true">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:text="药局名称"
        android:textColor="#424242"
        android:textSize="30dp"
        app:layout_constraintBottom_toTopOf="@+id/layout_adult"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layout_adult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/layout_child"
        app:layout_constraintTop_toBottomOf="@+id/tv_name">

        <TextView
            android:id="@+id/tv_adult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="成人口罩"
            android:textSize="20dp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_adult_amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="口罩数量"
            android:textSize="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv_adult" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layout_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/layout_adult"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_name">

        <TextView
            android:id="@+id/tv_child"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="小孩口罩"
            android:textSize="20dp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_child_amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="口罩数量"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv_child" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

自定义 MainAdapter

修改 MainAdapter.kt,将大人与小孩的口罩数量,绑定到指定元件显示。

...
...
...
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.itemViewBinding.tvName.text = pharmacyList[position].properties.name

        holder.itemViewBinding.tvAdultAmount.text = pharmacyList[position].properties.mask_adult.toString()
        holder.itemViewBinding.tvChildAmount.text = pharmacyList[position].properties.mask_child.toString()
    }
    
...
...
...

输出结果

参考资料

HKT 线上教室
https://tw-hkt.blogspot.com/

Freepik
https://www.freepik.com/

Create a List with RecyclerView
https://developer.android.com/guide/topics/ui/layout/recyclerview

RecyclerView Reference
https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView

Android RecyclerView Sample (Kotlin)
https://github.com/android/views-widgets-samples/tree/master/RecyclerViewKotlin


那今天【iThome 铁人赛】就介绍到这边罗~

顺带一提,KT 线上教室,脸书粉丝团,会不定期发布相关资讯,不想错过最新资讯,不要忘记来按赞,追踪喔!也欢迎大家将这篇文章分享给更多人喔。

我们明天再见罗!!!掰掰~


<<:  Day 14 - Rancher - 其他事项

>>:  day6 - proto buffer 简介 & spec 编写

Day 03: Python开发环境 Spyder初探

那麽在上一篇已经有安装好了Anaconda了,我们就可以开始使用Anaconda开发环境底下的各式功...

[ Day 02 ] 什麽是 React.js ?

我自己在学习新技术的时候比较习惯先了解一下这个技术的基本概念还有它解决了什麽问题。 所以今天我要跟...

Day14. Module & #extend #prepend #include - Ruby 继承 part1

Day14-15 一共会介绍 Ruby的2类、4种继承方式。 在Day2 我们提到 Ruby 为单一...

[Android Studio] -- Day 5 主题变换Theme02

前言 今天读到Theme主题实作,刚好又是app主题色系变化。这次采用setTheme和Style来...

从零开始学3D游戏设计:环境後制效果

这是 Roblox 从零开始系列,游戏环境章节的第一个单元,你将学会如何去针对场景进行後制 【You...