Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách thêm footer vào ListView trên Android bằng Kotlin

Giới thiệu

Hướng dẫn này trình bày cách thêm footer view (chân trang) vào ListView trên Android sử dụng Kotlin. Footer thường dùng để hiển thị "End of list", nút "Load more", hoặc quảng cáo.

Các bước thực hiện

Bước 1: Tạo dự án mới

Tạo project: File → New Project.

Bước 2: Thiết kế giao diện chính (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Bước 3: Tạo layout footer (res/layout/listview_footer.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16sp"
        android:text="Footer: End of the ListView!"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Bước 4: Xử lý logic (src/MainActivity.kt)

import android.os.Bundle
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var listView: ListView

    private val country = arrayOf(
        "India", "Australia", "South Africa", "North America",
        "South America", "Iceland", "GreenLand"
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        listView = findViewById(R.id.listView)

        // Tạo adapter
        val adapter = ArrayAdapter(
            this,
            android.R.layout.simple_list_item_1,
            country
        )

        // Inflate footer view
        val footer = layoutInflater.inflate(R.layout.listview_footer, listView, false) as ViewGroup

        // Thêm footer VỚI adapter
        listView.addFooterView(footer)

        // Gán adapter
        listView.adapter = adapter
    }
}

Bước 5: Cấu hình Manifest (androidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.q11">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. ListView sẽ hiển thị danh sách quốc gia với footer "Footer: End of the ListView!" ở cuối, cuộn cùng với list.

Lưu ý quan trọng

  • Phải gọi addFooterView() TRƯỚC khi set adapter. Nếu gọi sau, footer sẽ không hiển thị.
  • Footer không thể click được như item bình thường trừ khi bạn xử lý trong OnItemClickListener (footer sẽ trả về position = count - 1).
  • Có thể thêm nhiều footer bằng cách gọi addFooterView() nhiều lần.
  • Tương tự có addHeaderView() cho header.

Khuyến nghị: Dùng RecyclerView

ListView đã cũ. Với RecyclerView, footer/header xử lý linh hoạt hơn qua ConcatAdapter (AndroidX) hoặc nhiều view type trong adapter.

// RecyclerView với ConcatAdapter (API 31+ / AndroidX)
val listAdapter = CountryAdapter(countryList)
val footerAdapter = FooterAdapter()
val concatAdapter = ConcatAdapter(listAdapter, footerAdapter)
recyclerView.adapter = concatAdapter