Android中ListView分頁是比較常用的功能,當用戶從網絡上讀取信息時候,如果一下子加載全部信息這將耗費比較長的時間,造成不好的用戶體驗,同時一屏的內容也不足以顯示如此多的內容。這時候,我們就可以採用ListView的分頁。通過分頁分次加載數據,用戶看多少就去加載多少。
通常這也分爲三種方式,一種是設置一個按鈕,用戶點擊即加載。一種是當用戶滑動到底部時自動加載(需要SrcollView查看爲IDLE狀態並比較已顯示數和總數據)。還有一種是使用與下拉刷新類似的上拉刷新。
通常這也分爲三種方式,一種是設置一個按鈕,用戶點擊即加載。一種是當用戶滑動到底部時自動加載(需要SrcollView查看爲IDLE狀態並比較已顯示數和總數據)。還有一種是使用與下拉刷新類似的上拉刷新。
本文實現第一種。
最終效果如圖示:
當沒有數據時查看更多消失,本文設定了58個消息
主頁面的ListView佈局
< RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
>
<ListView
android:id= "@+id/mylv"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
/>
</ RelativeLayout>
ListView的items佈局:
<? xml version= "1.0" encoding= "utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
<TextView
android:id= "@+id/title"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_marginLeft= "10dp"
/>
<TextView
android:id= "@+id/content"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_marginLeft= "20dp"
/>
</ LinearLayout>
此頁面顯示是類似新聞客戶端的ListView,一個是標題,一個是內容。
ListView的底部佈局:
<? xml version= "1.0" encoding= "utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
<Button
android:id= "@+id/btn"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:text ="查看更多"
/>
</ LinearLayout>
一個簡單的Button,顯示爲查看更多。
適配器代碼:
public class MyAdapter extends BaseAdapter{
private List<News> myList;
private Context context;
public MyAdapter(List<News> _myList, Context _context) {
this. myList = _myList;
this. context = _context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myList.size();
}
@Override
public Object getItem( int arg0) {
// TODO Auto-generated method stub
return myList.get(arg0);
}
@Override
public long getItemId( int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView( int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
//此處可用Holder模式優化,偷個懶不寫了
arg1 = LayoutInflater.from( context).inflate(R.layout. lv_items, null);
TextView title = (TextView) arg1.findViewById(R.id.title );
TextView content = (TextView) arg1.findViewById(R.id.content );
title.setText( myList.get(arg0).getTitle());
content.setText( myList.get(arg0).getContent());
return arg1;
}
//查看更多時添加響應內容
public void addItems(News _news ){
myList.add( _news);
}
}
新聞實體類:
public class News {
private String title;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this. title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this. content = content;
}
}
主頁面的代碼:
public class MainActivity extends Activity {
private ListView mylv;
//總數據長度
private int dataSize = 58;
private MyAdapter adapter;
//底部的查看更多
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
List<News> news = new ArrayList<News>();
for( int i = 0; i < 10; i++){
News items = new News();
items.setTitle( "Title"+i);
items.setContent( "This is News Content" +i);
news.add(items);
}
mylv = (ListView)findViewById(R.id. mylv);
//初始化底部的查詢更多按鈕
view = getLayoutInflater().inflate(R.layout.more_view , null);
Button btn = (Button) view.findViewById(R.id. btn);
btn.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//加載更多數據
loadMore();
}
});
mylv.addFooterView( view);
//設置適配器
adapter = new MyAdapter(news, MainActivity. this);
mylv.setAdapter( adapter);
}
//獲取更多數據,實際開發中需要去從數據庫當中取出來
private void loadMore(){
int count = adapter.getCount();
//如果未顯示的數目小於10的話一次性加載全部
if(count + 10 > dataSize){
for( int i = count; i < dataSize; i++){
News items = new News();
items.setTitle( "Title"+i);
items.setContent( "This is News Content"+i);
adapter.addItems(items);
}
//去除查看更多的view
mylv.removeFooterView( view);
} else{
//每次增加10個
for( int i = count; i < count + 10; i++){
News items = new News();
items.setTitle( "Title"+i);
items.setContent( "This is News Content"+i);
adapter.addItems(items);
}
}
adapter.notifyDataSetChanged();
}
}