http://erwin-king.iteye.com/blog/1066632
我们在使用listview时难免会遇到分页的情况,既然是分页,那么能给用户看见分页的情况,无疑让用户在等待下一页数据的时候会有好处的,如果获取下一页时间过久的话,用户会觉得好像死机了,这样就会给用户一个不好的体验,下面我就来实现在listview分页是现实进度条,由于这个效果的要求不高,我只使用了很简单的方式来实现,高手莫笑哦!
1、首先便是xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main" android:orientation="vertical"
android:background="@drawable/mainapp_ic_bg" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:gravity="center_horizontal" android:layout_above="@+id/main_progessbar" android:layout_alignParentTop="true"
android:orientation="vertical" android:id="@+id/top" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="学习" android:background="@drawable/com_ttb_1l_bg"
android:id="@+id/main_title"
android:layout_width="fill_parent" android:gravity="center"
android:layout_height="wrap_content" android:textSize="18dip"
android:textColor="@color/white" android:visibility="gone"/>
<ListView android:id="@+id/main_listview" android:layout_below="@+id/main_title" android:cacheColorHint="#00000000"
android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
</LinearLayout>
<LinearLayout android:gravity="center" android:layout_alignParentBottom="true"
android:background="@color/black" android:id="@+id/main_progessbar" android:layout_width="fill_parent"
android:visibility="gone" android:layout_height="wrap_content" >
<ProgressBar android:layout_width="wrap_content"
android:layout_height="wrap_content"></ProgressBar>
<TextView android:text="加载中..." android:layout_width="wrap_content"
android:paddingLeft="10dip" android:gravity="center" android:layout_height="fill_parent"></TextView>
</LinearLayout>
</RelativeLayout>
2、activity类
public class ListView_ProgressBar extends Activity implements OnScrollListener {
int lastItem = 0;
int mProgressStatus = 0;
private Handler mHandler = new Handler();
ProgressBar progressBar;
MyAdapter myAdapter;
List<String> str = new ArrayList<String>();
int pageNo = 1;
ListView listview;
int totalPage = 10;
int totalRecord = 200;
LinearLayout main_progessbarn;
private boolean isScrolling = false;
int now = 0;
boolean isPagination = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
main_progessbarn = (LinearLayout)findViewById(R.id.main_progessbar);
listview = (ListView) findViewById(R.id.main_listview);
for (int i = 0; i < 15; i++) {
str.add("条目内容" + (pageNo-1)*20+i);
}
myAdapter = new MyAdapter(ListView_ProgressBar.this, str);
listview.setAdapter(myAdapter);
listview.setOnScrollListener(this);
}
public void onScroll(AbsListView v, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
lastItem = firstVisibleItem + visibleItemCount - 1;
System.out.println("lastItem:" + lastItem);
}
public void onScrollStateChanged(AbsListView v, int state) {
if (lastItem == str.size()-1
&& state == OnScrollListener.SCROLL_STATE_IDLE) {
pageNo++;
if (pageNo <= totalPage) {
ccc(pageNo);
}
}
}
private void ccc(final int pageNo){
main_progessbarn.setVisibility(View.VISIBLE);
new Thread() {
public void run() {
getStringList(pageNo);
mHandler.post(refreshAdapter);
}
}.start();
}
private Runnable refreshAdapter = new Runnable() {
public void run() {
myAdapter.notifyDataSetChanged();
main_progessbarn.setVisibility(View.GONE);
}
};
public void getStringList(int pageNo) {
for (int i = 0; i < 10000; i++) {
str.add("条目内容" + (pageNo-1)*20+i);
}
}
}
3、listview适配器
public class MyAdapter extends BaseAdapter {
Context context;
List<String> list;
LayoutInflater lif;
public MyAdapter(Context context, List<String> list,) {
this.context = context;
this.list = list;
}
public int getCount() {
if (list != null) {
return list.size();
} else {
return 0;
}
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
// mHandler.post(mScrollToButton);//传�?�?��消息进行滚动
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final MyView mv;
if (convertView == null) {
mv = new MyView();
lif = LayoutInflater.from(context);// 转化到context这个容器
convertView = lif.inflate(R.layout.main_item, null);// 设置要转化的layout文件
} else {
mv = (MyView) convertView.getTag();
}
mv.tv1 = (TextView) convertView.findViewById(R.id.main_listview_item);// 取得实例
System.out.println("条目内容" + list.get(position));
mv.tv1.setText(list.get(position));
if ((position % 2) == 0) {
convertView.setBackgroundResource(R.drawable.com_list_bg_d);
} else {
convertView.setBackgroundResource(R.drawable.com_list_bg_s);
}
convertView.setTag(mv);
return convertView;
}
private static class MyView {
TextView tv1;
}
}