前言:
ListView在Android手機是一個常用的UI設計,使用起來也相當方便,但是當大量資料存放到ListView時,為了有良好的使用者體驗,會考慮分批載入資料的處理方式,這裡稱為Paging。本文提供一個最陽春的範例,說明Paging的效果。
成果:
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<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" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.chengyu.paginglistview.MainActivity"> | |
<ListView | |
android:id="@+id/lv" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chengyu.paginglistview; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.widget.AbsListView; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class MainActivity extends Activity implements AbsListView.OnScrollListener { | |
private ListView list = null; | |
private int page = 0; | |
private int total = 100; // assume that we have 100 data. | |
private boolean isLoading = false; | |
private ArrayList<Map<String, String>> data = null; | |
private PagingListAdapter mPLAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
list = (ListView) findViewById(R.id.lv); | |
data = new ArrayList<Map<String, String>>(); // data | |
mPLAdapter = new PagingListAdapter(this, data); | |
list.setAdapter(mPLAdapter); | |
list.setOnScrollListener(this); | |
//load data | |
nextPage(); | |
// | |
} | |
@Override | |
public void onScrollStateChanged(AbsListView view, int scrollState) { | |
} | |
@Override | |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | |
if (totalItemCount >0 && list != null) { | |
int lastPosition = list.getLastVisiblePosition(); | |
if (lastPosition + 1 == totalItemCount && data.size() < total) { | |
// loading | |
nextPage(); | |
} | |
} | |
} | |
private void nextPage() { | |
if (!isLoading) { | |
Toast.makeText(MainActivity.this, "Loading...", Toast.LENGTH_SHORT).show(); | |
for (int i=0; i<20; i++) { | |
Map<String, String> addData = new HashMap<String, String>(); | |
addData.put("msg1", String.valueOf(data.size()) + ". msg1"); | |
addData.put("msg2", String.valueOf(data.size()) + ". msg2"); | |
data.add(addData); | |
} | |
isLoading = false; | |
mPLAdapter.refreshData(data); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chengyu.paginglistview; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.Map; | |
/** | |
* Created by chengyu on 2016/02/05. | |
*/ | |
class PagingListAdapter extends BaseAdapter { | |
private ArrayList<Map<String, String>> data = null; | |
private Context context = null; | |
private LayoutInflater inflater; | |
private class ViewHolder { | |
TextView tv1; | |
TextView tv2; | |
} | |
public PagingListAdapter(Context context, ArrayList<Map<String, String>> data) { | |
this.data = data; | |
this.context = context; | |
} | |
@Override | |
public int getCount() { | |
return (data == null) ? 0 : data.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
return (data == null) ? null : data.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public boolean isEnabled(int position) { | |
return true; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View view = convertView; | |
final ViewHolder holder; | |
if (view == null) { | |
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
view = inflater.inflate(R.layout.list_item, null); | |
holder = new ViewHolder(); | |
holder.tv1 = (TextView) view.findViewById(R.id.list_item_tv1); | |
holder.tv2 = (TextView) view.findViewById(R.id.list_item_tv2); | |
view.setTag(holder); | |
} else { | |
holder = (ViewHolder) view.getTag(); | |
} | |
holder.tv1.setText(data.get(position).get("msg1")); | |
holder.tv2.setText(data.get(position).get("msg2")); | |
return view; | |
} | |
/** | |
* refresh data | |
* @param newData | |
*/ | |
public void refreshData(ArrayList<Map<String, String>> newData) { | |
this.data = newData; | |
this.notifyDataSetChanged(); | |
} | |
} |
留言
張貼留言