转:ListView 实现更多按钮

转载自:http://blog.csdn.net/zhiyou007/article/details/6524291

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 

android:layout_width="fill_parent" 

android:gravity="center_horizontal" 

android:padding="3dp" 

android:layout_height="fill_parent"> 


<TextView 

android:id="@id/android:empty" 

android:layout_width="wrap_content" 

android:layout_height="fill_parent" 

android:gravity="center" 

android:padding="5dp" 

android:text="Loading more ..."/> 



</LinearLayout> 
 

 

 

foot.xml
MoreListTestActivity
public class MoreListTest extends ListActivity {

private ListView mListView;

private ArrayList<String> mItems;

private ArrayAdapter<String> mAdapter;

private boolean loadingMore = false;

private int itemsCount = 15; //新添加的数据个数



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

/* 添加初始显示的数据 */

addData();

mListView = getListView();//获得listview对象



/* 将footview的布局转换成View对象*/

View foot = ((LayoutInflater) this

.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(

R.layout.foot, null, false);



/* 给footview添加点击事件监听器*/

foot.setOnClickListener(new View.OnClickListener() {



@Override

public void onClick(View v) {

loadingMore =true;

for (int i = 0; i < itemsCount; i++) {

mItems.add("new "+i); //添加数据

}

mAdapter.notifyDataSetChanged(); //通知适配器重新适配

}

});



/* 给listview添加footview*/

mListView.addFooterView(foot);

mAdapter = new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_1, mItems);

/* listview添加适配器*/

mListView.setAdapter(mAdapter);

}



private void addData() {

mItems = new ArrayList<String>();

for (int i = 0; i < 5; i++) {

mItems.add(i+"");

}

}



}
  效果图片   效果图片

 

你可能感兴趣的:(android)