当ListView的数据需要分页加载时,通常会在ListView滚动到底部时加上一个More的TextView或一个进度条。
1.添加TextView,当滚动到底部Button出现时,点击Button加载下一页的数据,只要给Button添加点击事件去获取数据即可。
2.添加进度条,当滚到底部出现进度条时,这个时候要进行位置判断,判断进度条出现的位置坐标,符合条件就去获取数据。
XML文件:
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000" >
</ListView>
</LinearLayout>
More的 TextView和进度条都可以不写xml文件,直接在java代码中加,但是我偏好些xml文件,布局修改方便。
more.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/more"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="onClick"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="更多" android:textSize="20dp" android:textStyle="bold" android:gravity="center" android:minHeight="60dp"/>
</LinearLayout>
进度条文件more.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
ListActivity.java文件代码:
public class ListActivity extends Activity {
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customer);
list = (ListView) findViewById(R.id.list_customer);
initeListView();
initRadios();
setMoreCtrl(false);
getCustomerFromServer();
CommonUtility.showLoading(this, null, "正在加载数据...");
}
private void setMoreCtrl(boolean isLocal) {
if (more == null) {
more = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.more, null);
more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
++curPage;
getCustomerFromServer();
}
});
list.addFooterView(more);
}
}
private void getCustomerFromServer() {
CommonUtility.showLoading(this, null, "正在加载数据...");
getResourceTask = new Thread() {
public void run() {
Log.d("", "curPage:" + curPage);
if (curPage == 1) {
myList = DataTaker.getCustomers(isReach, curPage);
} else if (curPage > 1) {
if (myList == null)
myList = new ArrayList<Customer>();
myList.addAll(DataTaker.getCustomers(isReach, curPage));
}
if (myList != null) {
runOnUiThread(new Runnable() {
public void run() {
if (myList != null) {
if (adapter == null) {
adapter = new AppsAdapter();
list.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
getResourceTask = null;
}
});
}
CommonUtility.cancelLoading();
}
};
getResourceTask.start();
}
class WebAdapter extends BaseAdapter {
@Override
public int getCount() {
return myList.size();
}
@Override
public Object getItem(int position) {
return myList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Customer customer = myList.get(position);
CheckBox chk = null;
if (convertView == null) {
convertView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.customer_item, null);
chk = ((CheckBox) convertView.findViewById(R.id.chb));
chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int i = Integer.valueOf(buttonView.getTag().toString());
myList.get(i).isChecked = isChecked;
}
});
}
if (chk == null) {
chk = ((CheckBox) convertView.findViewById(R.id.chb));
}
chk.setTag(position);
chk.setChecked(customer.isChecked);
((TextView) convertView.findViewById(R.id.txt_flow)).setText(String
.valueOf(customer.flow));
((TextView) convertView.findViewById(R.id.txt_time)).setText(String
.valueOf(customer.main_call_duration));
float last_flow = 0;
if (customer.last_3_customer_flows != null
&& customer.last_3_customer_flows.size() > 0)
last_flow = customer.last_3_customer_flows.get(0).flow;
((TextView) convertView.findViewById(R.id.txt_last_flow))
.setText(String.valueOf(last_flow));
String os = "未知";
if (customer.os_name != null && !customer.os_name.equals(""))
os = customer.os_name;
((TextView) convertView.findViewById(R.id.txt_os)).setText(os);
((TextView) convertView.findViewById(R.id.txt_phone))
.setText(customer.phone_number);
return convertView;
}
}
}
对于TextView只要加上onClick监听事件就可以,上面的代码也写了。
下面的代码为添加进度条所用的:
list.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& list.getLastVisiblePosition() == list.getCount() - 1) {
int[] location = new int[2];
if (more != null)
more.getLocationOnScreen(location);
if (location[1] > list.getBottom() - 20) {
getResource();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
这个位置监听,当滚动到底部进度条出现时就会继续加载了