【Android 入門開發實戰:口罩地圖】RecyclerView 進階項目佈局

 

【Android 入門開發實戰:口罩地圖】線上免費講義課程目錄

在上一節當中,我們使用了一個 TextView,顯示藥局名稱,這次我們想要讓,項目顯示更多口罩重點資訊,所以我們將進行調整項目佈局。

預期畫面

置中顯示藥局名稱與口罩數量

項目佈局

修改 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()
    }
    
...
...
...

輸出結果

參考資料

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

程式碼範例

範例名稱:RecyclerView 進階項目佈局
開發人員:HKT (侯光燦)
程式語言:Kotlin
開發環境:Android Studio 4.1.2 & Android 11 & Kotlin 1.4.21
授權範圍:使用時必須註明出處且不得為商業目的之使用
範例下載點:點我下載

這個網誌中的熱門文章

16天記下7000單字

2022 最新入門零基礎 Flutter教學 【Flutter 程式設計入門實戰 30 天】Flutter 教學課程目錄 (IntelliJ IDEA 開發教學)

2023 最新入門零基礎 Kotlin教學【從零開始學 Kotlin 程式設計】Kotlin 教學課程目錄 (Android Kotlin, IntelliJ IDEA, Android Studio, Android APP 開發教學)

nano 文字編輯器

最新入門零基礎 Java 教學【從零開始學 Java 程式設計】Java教學課程目錄 (IntelliJ IDEA 開發教學)