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

Thêm footer cho ListView trong Android

Thêm footer cho ListView trong Android

Hướng dẫn này minh họa cách thêm một view footer (chân trang) vào ListView Android.

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

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

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

<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">
    </ListView>
</RelativeLayout>

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

Tạo file layout mới tên listview_footer.xml trong res/layout/:

<?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="match_parent">
    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Footer: End of the ListView!"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:padding="16sp" />
</LinearLayout>

Bước 4: Code 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[] country = new String[] {
        "India", "Australia", "South Africa", "North America",
        "South America", "Iceland", "GreenLand"
    };
    List<String> LISTSTRING;
    LayoutInflater layoutInflater;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        listView = findViewById(R.id.listView);
        LISTSTRING = new ArrayList<>(Arrays.asList(country));
        
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, LISTSTRING);
        
        layoutInflater = getLayoutInflater();
        ViewGroup footer = (ViewGroup) layoutInflater.inflate(R.layout.listview_footer, listView, false);
        listView.addFooterView(footer);
        listView.setAdapter(adapter);
    }
}

Bước 5: Cấu hình AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
    <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ị, chạy app. ListView sẽ hiển thị danh sách các quốc gia với footer "Footer: End of the ListView!" ở cuối danh sách.

Mẹo: Phương thức addFooterView() phải được gọi trước setAdapter(). Footer sẽ cuộn cùng với danh sách và không thể tách biệt.