Android UI設計--ListView的頁腳(footer)的使用


有時候使用ListView顯示一些數據時,希望在列表項的尾部增加一個頁腳(注:不是放在屏幕的最低端),頁腳會隨着ListView的數量的增加而自動跟隨,由於ListView在數量超過屏幕顯示的數量的時候,導致你使用在布局中layout_below某個布局下失效(如果ListView數量少於屏幕顯示數量,則顯示頁腳,否則將被覆蓋)。

實現方式有兩種,一種是通過ScrollView里面嵌套布局實現,另一種是通過ListView的addFooterView()方法實現,第一種google官方不推薦。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">
    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

</LinearLayout>

list_footer.xml,這個就是你要在ListView尾部顯示的布局樣式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/footer"
    android:background="@drawable/a_device_title_bar"
    android:orientation="vertical" >
    

</LinearLayout>

MainActivity.java

package com.example.listviewfooter;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    private ListView lv;
    private ArrayList<String> list = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView)findViewById(R.id.listview);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_expandable_list_item_1,
                getData());
 View footerView = ((LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.list_footer, null, false); lv.addFooterView(footerView);
        lv.setAdapter(adapter);
    }
    
    private ArrayList<String> getData()
    {
        for(int i=0;i<5;i++)
        {
            list.add("hualang");
        }
        return list;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

主要是通過LayoutInflater加載一個View並添加到ListView上即可。

注意,setFooterView方法必須在setAdapter方法前設置,否則不顯示footer

下面是顯示20條ListView和顯示5條ListView的不同效果,footer一直在ListView尾部


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM