listView 添加addHeaderView和addFooterView以及handler.postDelayed的使用

Java代码   
  1. import android.app.ListActivity;  
  2. import android.content.Context;  
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.os.SystemClock;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.AdapterView;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.Button;  
  11. import android.widget.ListView;  
  12. import android.widget.TextView;  
  13. import java.util.Arrays;  
  14. import java.util.Collections;  
  15. import java.util.List;  
  16. import java.util.concurrent.atomic.AtomicBoolean;  
  17.   
  18. public class HeaderFooterDemo extends ListActivity {  
  19.     private static String[] items={"lorem",  
  20.                                                                     "pellentesque""augue",  
  21.                                                                     "purus"};  
  22.     private long startTime=SystemClock.uptimeMillis();  
  23.     private Handler handler=new Handler();  
  24.     private AtomicBoolean areWeDeadYet=new AtomicBoolean(false);  
  25.       
  26.     @Override  
  27.     public void onCreate(Bundle icicle) {  
  28.         super.onCreate(icicle);  
  29.         setContentView(R.layout.main);  
  30.         getListView().addHeaderView(buildHeader());  
  31.         getListView().addFooterView(buildFooter());  
  32.         setListAdapter(new ArrayAdapter<String>(this,  
  33.                                                 android.R.layout.simple_list_item_1,  
  34.                                                 items));  
  35.     }  
  36.       
  37.     @Override  
  38.     public void onDestroy() {  
  39.         super.onDestroy();  
  40.           
  41.         areWeDeadYet.set(true);  
  42.     }  
  43.       
  44.     private View buildHeader() {  
  45.         Button btn=new Button(this);  
  46.           
  47.         btn.setText("Randomize!");  
  48.         btn.setOnClickListener(new View.OnClickListener() {  
  49.             public void onClick(View v) {  
  50.                 List<String> list=Arrays.asList(items);  
  51.                   
  52.                 Collections.shuffle(list);  
  53.                   
  54.                 setListAdapter(new ArrayAdapter<String>(HeaderFooterDemo.this,  
  55.                                                         android.R.layout.simple_list_item_1,  
  56.                                                         list));  
  57.             }  
  58.         });  
  59.           
  60.         return(btn);  
  61.     }  
  62.       
  63.     private View buildFooter() {  
  64.         TextView txt=new TextView(this);  
  65.           
  66.         updateFooter(txt);  
  67.           
  68.         return(txt);  
  69.     }  
  70.       
  71.     private void updateFooter(final TextView txt) {  
  72.         long runtime=(SystemClock.uptimeMillis()-startTime)/1000;  
  73.           
  74.         txt.setText(String.valueOf(runtime)+" seconds since activity launched");  
  75.           
  76.         if (!areWeDeadYet.get()) {  
  77.             handler.postDelayed(new Runnable() {  
  78.                 public void run() {  
  79.                   
  80.                     updateFooter(txt);    
  81.                 }  
  82.             }, 1000);  
  83.         }  
  84.     }  
  85. }  

 

Java代码   
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ListView  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@android:id/list"  
  5.     android:layout_width="fill_parent"   
  6.     android:layout_height="fill_parent"  
  7.     android:drawSelectorOnTop="true"  
  8. />  

 android:drawSelectorOnTop="false"注意这句话的意思 如果你设为true,那么选中的颜色会在上面 挡住button

设为false 则选中颜色在下面可以看见button 自己测试一下就明白了

 

你可能感兴趣的:(ListView)