Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีเพิ่ม Footer ให้ ListView ใน Android

บทความนี้สาธิตวิธีเพิ่ม Footer View ให้กับ ListView ใน Android โดยใช้ addFooterView() ก่อนเซ็ต Adapter

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout หลัก (activity_main.xml)

วาง ListView เต็มหน้าจอ:

<?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"
    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>

ขั้นตอนที่ 3: สร้าง Layout สำหรับ Footer (listview_footer.xml)

คลิกขวาที่ res/layout → New → Layout Resource File ชื่อ listview_footer.xml:

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Footer: End of the ListView!"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#666666" />

</LinearLayout>

ขั้นตอนที่ 4: เขียนโค้ด MainActivity.java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    ListView listView;
    String[] countries = {"India", "Australia", "South Africa", "North America", "South America", "Iceland", "GreenLand"};
    List<String> countryList;
    LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listView);
        countryList = new ArrayList<>(Arrays.asList(countries));

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, countryList);

        // สำคัญ: Inflate footer และ addFooterView ก่อน setAdapter
        inflater = getLayoutInflater();
        ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.listview_footer, listView, false);
        listView.addFooterView(footer);

        listView.setAdapter(adapter);
    }
}

⚠️ จุดสำคัญ: ลำดับการทำงาน

  1. inflate footer view
  2. listView.addFooterView(footer) - ต้องเรียก ก่อน setAdapter
  3. listView.setAdapter(adapter)

หากเรียก addFooterView หลัง setAdapter จะเกิด Crash: IllegalStateException: Cannot add footer view after setAdapter

Header View (เพิ่มเติม)

ใช้วิธีเดียวกันสำหรับ Header:

ViewGroup header = (ViewGroup) inflater.inflate(R.layout.listview_header, listView, false);
listView.addHeaderView(header);

ListView vs RecyclerView

สำหรับโปรเจกต์ใหม่ แนะนำใช้ RecyclerView แทน ซึ่งยืดหยุ่นกว่า และรองรับ Header/Footer ผ่าน ConcatAdapter หรือ view type หลายแบบ