Android:Android ListActivity with a header or footer(为ListView添加header和footer)

转自:http://www.samcoles.co.uk/mobile/android-listactivity-with-a-header-or-footer/


This post will show you how to go about implementing a ListActivity with a header or footer that does not follow the same layout as the list rows. A well known and obvious example of this is the main screen on the Android Market.

First, create a class that extends ListActivity:

package samcoles.listwithheader;   
import android.app.ListActivity; 
import android.os.Bundle;   
public class ListWithHeader extends ListActivity {    
 /** Called when the activity is first created. */    
@Override    public void onCreate(Bundle savedInstanceState) {         
super.onCreate(savedInstanceState);         
setContentView(R.layout.list_main);     } }

Remember to ensure you have added this activity to your AndroidManifest.xml.

Next up you’ll need to create the list_main layout that’s set in the above ListActivity within /res/layout. Remember the<ListView> must haveid=”@+id/android:list” and you should also have a view withid=”@+id/android:empty” to display when there’s no items in the list. Your ListActivity will go looking for these and your app will crash when loading this activity if you don’t have them. If you’re not aware of this then the debugger can be a bit unhelpful. The TextView uses a string resource, empty_list that you’ll need to remember to add too if you’re using this.

here’s the list_main layout:

<?xml version="1.0" encoding="utf-8"?>       
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:orientation="vertical">  
 <ListView    android:id="@+id/android:list"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:scrollbars="none"/>   <TextView    android:id="@+id/android:empty"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/empty_list" />
 </LinearLayout>

You’ll also need a layout for the header, note that this below references a couple of string resources and an image resource:

list_header.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:id="@+id/header_layout_root"  android:background="#ffffff"  android:orientation="vertical">     
<TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/app_name"        android:background="#000000"        android:textColor="#ffffff"        android:textSize="28dp"        android:gravity="center_horizontal"        android:paddingTop="10dp"        android:paddingBottom="10dp">     </TextView>     
<RelativeLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content">        
 <ImageView            android:id="@+id/sample_image"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:adjustViewBounds="true"            android:src="@drawable/sample_image"            android:scaleType="fitXY">         </ImageView>        
 <TextView            android:id="@+id/sample_title"            android:text="@string/sample_text"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="#ffffffff"            android:textSize="12dp"            android:background="#AA000000"            android:padding="5dp"            android:layout_alignParentLeft="true"            android:layout_alignBottom="@id/sample_image">         </TextView>    
 </RelativeLayout> 
</LinearLayout>

Next go back to your ListActivity class and it’s time to put in the code to add the header to your list, place this just below the call tosetContentView():

ListView lv = getListView(); 
LayoutInflater inflater = getLayoutInflater(); 
View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root)); 
lv.addHeaderView(header, null, false);

To explain the above, first we call getListView() to get the activity’sListView and assign this to an object. We then callgetLayoutInflater() and do the same – this gets theLayoutInflater that’s attached to the current application context. TheLayoutInflater is then used to instantiate the layout xml file so that we have aView object that, in line 4, we can attach to theListView! There is also a methodListView.addFooterView() – you use this in exactly the same way asListView.addHeaderView().

The full ListActivity class is shown below, it also puts some items in the list.

ListWithHeader.java

package samcoles.listwithheader;   
import android.app.ListActivity; 
import android.os.Bundle; import android.view.LayoutInflater; 
import android.view.View; import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView;   
public class ListWithHeader extends ListActivity {  
   /** Called when the activity is first created. */   
 @Override    public void onCreate(Bundle savedInstanceState) {        
 super.onCreate(savedInstanceState);         
setContentView(R.layout.list_main);           
//add header to list         
ListView lv = getListView();        
 LayoutInflater inflater = getLayoutInflater();    
 View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root));        
 lv.addHeaderView(header, null, false);           
//add some list items         
String listItems[] = {"List Item One", "List Item Two", "List Item Three", "List Item Four", "List Item Five"};        
 lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems));     } }

And here’s a look at that in the emulator:

ListWithHeader



 
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值