Android uidesign-Use of the footer of listview

Source: Internet
Author: User

Sometimes, when using listview to display some data, you want to add a footer at the end of the list item (Note: it is not at the lowest end of the screen). The footer will automatically follow as the number of listview increases, when the number of listview displays exceeds the number of screens, the layout_below layout becomes invalid. (if the number of listview views is less than the number of screens, the footer is displayed, otherwise it will be overwritten ).

There are two implementation methods: one is implemented through nested layout in scrollview, and the other is implemented through the addfooterview () method of listview. The first method is not officially recommended by 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. This is the layout that you want to display at the end of the 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;    }}

You can load a view through layoutinflater and add it to the listview.

Note: The setfooterview method must be set before the setadapter method. Otherwise, the footer is not displayed.

The following shows the different effects of displaying 20 listview items and 5 listview items. footer is always at the end of listview.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.