ListView 分页运用
-------------------------------.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
---------------------------.java
package cn.com;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class MainAct extends ListActivity implements OnScrollListener {
ListView list;
int scrollState;
int count = 400;
int lastItem;
int visibleItemCount;
Button footerButton;
LinearLayout footerProgressBarLayout;
View view;
ListAdapter listAdapter = new ListAdapter();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = LayoutInflater.from(this);
view = inflater.inflate(R.layout.other_listview_footer_more, null);
footerButton = (Button) view.findViewById(R.id.button);
footerProgressBarLayout = (LinearLayout) view
.findViewById(R.id.linearlayout);
footerProgressBarLayout.setVisibility(View.GONE);
footerButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (lastItem == listAdapter.count
&& scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
footerButton.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.VISIBLE);
if (listAdapter.count <= count) {
new Handler().postDelayed(new Runnable() {
public void run() {
listAdapter.count += 10;
listAdapter.notifyDataSetChanged();
footerButton.setVisibility(View.VISIBLE);
footerProgressBarLayout
.setVisibility(View.GONE);
}
}, 0);
}
}
}
});
list = getListView();
list.addFooterView(view);
list.setAdapter(listAdapter);
list.setOnScrollListener(this);
}
class ListAdapter extends BaseAdapter {
int count = 10;
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(MainAct.this);
View view = inflater.inflate(R.layout.other_listview_item, null);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText("Hello World " + position);
return view;
}
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.visibleItemCount = visibleItemCount;
lastItem = firstVisibleItem + visibleItemCount - 1;
System.out.println(listAdapter.count);
if (listAdapter.count >= count) {
list.removeFooterView(view);
}
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.scrollState = scrollState;
}
}