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

package com.myapp.android.test; 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.LayoutInflater; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.TextView; 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); View loadingView; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); loadingView = LayoutInflater.from(this).inflate(R.layout.loading, null); setContentView(R.layout.main); getListView().addHeaderView(buildHeader()); //getListView().addHeaderView(loadingView); 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源文件:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!--<ListView android:layout_width="fill_parent" android:cacheColorHint="#00000000" android:layout_height="wrap_content" android:id="@+id/listView" android:scrollbars="none" android:layout_weight="1"> </ListView>--> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:stackFromBottom="true" android:transcriptMode="normal"/> </LinearLayout>

 

你可能感兴趣的:(android,ListView,list,layout,button,encoding)