- import android.app.ListActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.SystemClock;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.TextView;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.concurrent.atomic.AtomicBoolean;
- public class HeaderFooterDemo 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>(HeaderFooterDemo.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 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="true"
- />
android:drawSelectorOnTop="false"注意这句话的意思 如果你设为true,那么选中的颜色会在上面 挡住button
设为false 则选中颜色在下面可以看见button 自己测试一下就明白了