scott0 2011-11-24
转载自:http://www.eoeandroid.com/thread-19467-3-1.html
第一种:滚动式分页【估计是你想要的那种】
packagecom.yangguangfu.listview;
importandroid.app.ListActivity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.AbsListView;
importandroid.widget.BaseAdapter;
importandroid.widget.LinearLayout;
importandroid.widget.ListView;
importandroid.widget.ProgressBar;
importandroid.widget.TextView;
importandroid.widget.AbsListView.OnScrollListener;
importandroid.widget.LinearLayout.LayoutParams;
/**
*
*@author阿福
*
*/
publicclassMainActivityextendsListActivityimplementsOnScrollListener{
privatestaticfinalStringTAG="MainActivity";
privatelistViewAdapteradapter=newlistViewAdapter();
privateintlastItem=0;
/**
*设置布局显示为目标有多大就多大
*/
privateLayoutParamsWClayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
/**
*设置布局显示目标最大化
*/
privateLayoutParamsFFlayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
privateProgressBarprogressBar;
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreate(BundlesavedInstanceState)");
//线性布局
LinearLayoutlayout=newLinearLayout(this);
//设置布局水平方向
layout.setOrientation(LinearLayout.HORIZONTAL);
//进度条
progressBar=newProgressBar(this);
//进度条显示位置
progressBar.setPadding(0,0,15,0);
layout.addView(progressBar,WClayoutParams);
TextViewtextView=newTextView(this);
textView.setText("加载中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(textView,FFlayoutParams);
layout.setGravity(Gravity.CENTER);
LinearLayoutloadingLayout=newLinearLayout(this);
loadingLayout.addView(layout,WClayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
//得到一个ListView用来显示条目
ListViewlistView=getListView();
//添加到脚页显示
listView.addFooterView(loadingLayout);
//
registerForContextMenu(listView);
//
setListAdapter(adapter);
listView.setOnScrollListener(this);
}
publicvoidonScroll(AbsListViewv,intfirstVisibleItem,
intvisibleItemCount,inttotalItemCount){
lastItem=firstVisibleItem+visibleItemCount-1;
System.out.println("lastItem:"+lastItem);
Log.i(TAG,"lastItem:"+lastItem);
}
publicvoidonScrollStateChanged(AbsListViewv,intstate){
if(lastItem==adapter.count
&&state==OnScrollListener.SCROLL_STATE_IDLE){
adapter.count+=10;
adapter.notifyDataSetChanged();
Log.i(TAG,"lastItem:"+lastItem);
}
}
/**
*要用用于生成显示数据
*@author阿福
*
*/
classlistViewAdapterextendsBaseAdapter{
intcount=10;
publicintgetCount(){
Log.i(TAG,"count:"+count);
returncount;
}
publicObjectgetItem(intpos){
Log.i(TAG,"pos:"+pos);
returnpos;
}
publiclonggetItemId(intpos){
returnpos;
}
publicViewgetView(intpos,Viewv,ViewGroupp){
TextViewview=newTextView(MainActivity.this);
view.setText("阿福播放器"+pos);
Log.i(TAG,"entry:"+pos);
view.setTextSize(20f);
view.setHeight(80);
returnview;
}
}
}
按钮式分页:
packagecom.yangguangfu.listview;
importandroid.app.ListActivity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.AbsListView;
importandroid.widget.BaseAdapter;
importandroid.widget.Button;
importandroid.widget.LinearLayout;
importandroid.widget.ListView;
importandroid.widget.TextView;
importandroid.widget.LinearLayout.LayoutParams;
/**
*
*@author阿福
*
*/
publicclassButtonLoadingActivityextendsListActivity{
privatestaticfinalStringTAG="ButtonLoadingActivity";
privatelistViewAdapteradapter=newlistViewAdapter();
/**
*设置布局显示为目标有多大就多大
*/
privateLayoutParamsWClayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
/**
*设置布局显示目标最大化
*/
privateLayoutParamsFFlayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreate(BundlesavedInstanceState):");
LinearLayoutlayout=newLinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
Buttonbutton=newButton(this);
button.setText("点击加载下五条...");
button.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(button,FFlayoutParams);
layout.setGravity(Gravity.CENTER);
LinearLayoutloadingLayout=newLinearLayout(this);
loadingLayout.addView(layout,WClayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
ListViewlistView=getListView();
listView.addFooterView(loadingLayout);
button.setOnClickListener(newButton.OnClickListener()
{@Override
publicvoidonClick(Viewv)
{
adapter.count+=5;
Log.i(TAG,"setOnClickListener:"+adapter.count);
adapter.notifyDataSetChanged();
}
});
setListAdapter(adapter);
}
publicvoidonScrollStateChanged(AbsListViewv,ints){
Log.i(TAG,"onScrollStateChanged(AbsListViewv,ints)");
}
classlistViewAdapterextendsBaseAdapter{
intcount=10;/*startingamount*/
publicintgetCount(){returncount;}
publicObjectgetItem(intpos){returnpos;}
publiclonggetItemId(intpos){returnpos;}
publicViewgetView(intpos,Viewv,ViewGroupp){
TextViewtextView=newTextView(ButtonLoadingActivity.this);
textView.setHeight(80);
textView.setTextSize(20);
textView.setText("阿福播放器"+pos);
Log.i(TAG,"getView:pos:"+pos);
returntextView;
}
}
}