List中加上加HeaderView和FootView

package wyf.ytl;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class Sample_8_3 extends ListActivity {
	private static String[] items = { "lorem", "pellentesque", "augue", "purus" };
	private long startTime = SystemClock.uptimeMillis();
	private Handler handler = new Handler();
	private AtomicBoolean areWeDeadYet = new AtomicBoolean(false);

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		getListView().addHeaderView(buildHeader());
		getListView().addFooterView(buildFooter());
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, items));
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		areWeDeadYet.set(true);
	}

	private View buildHeader() {
		Button btn = new Button(this);

		btn.setText("Randomize!");
		btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				List<String> list = Arrays.asList(items);

				Collections.shuffle(list);

				setListAdapter(new ArrayAdapter<String>(Sample_8_3.this,
						android.R.layout.simple_list_item_1, list));
			}
		});

		return (btn);
	}

	private View buildFooter() {
		TextView txt = new TextView(this);

		updateFooter(txt);

		return (txt);
	}

	private void updateFooter(final TextView txt) {
		long runtime = (SystemClock.uptimeMillis() - startTime) / 1000;

		txt.setText(String.valueOf(runtime)
				+ " seconds since activity launched");

		if (!areWeDeadYet.get()) {
			handler.postDelayed(new Runnable() {
				public void run() {

					updateFooter(txt);
				}
			}, 1000);
		}
	}
}

 xml

<?xml version="1.0" encoding="utf-8"?>   
<ListView   
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@android:id/list"  
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"  
    android:drawSelectorOnTop="false"  
/>  

 android:drawSelectorOnTop="false"   那个黄色背景在下面

http://wang-peng1.iteye.com/blog/578386

你可能感兴趣的:(android,xml,OS,Blog)