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尾部

posted on 2012-10-25 15:41  花郎V  阅读(6556)  评论(0编辑  收藏  举报