zoukankan      html  css  js  c++  java
  • 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尾部

  • 相关阅读:
    bzoj 1061 单纯形法,或转化网络流(待补)
    bzoj 1007 计算几何,单调栈
    bzoj 1015 并查集,离线
    bzoj 1013 高斯消元
    java类继承HttpServlet类实现Servlet程序出现405错误:HTTP method POST is not supported by this URL
    算法的特性和算法设计的要求
    Java实现自定义异常类
    怎么查看 MySQL 数据文件在当前电脑的存储位置
    数据结构的分类
    JS实现“全选”和"全不选"功能
  • 原文地址:https://www.cnblogs.com/loulijun/p/2738952.html
Copyright © 2011-2022 走看看