本文实例讲述了Android基于ListView实现类似Market分页加载效果。分享给大家供大家参考,具体如下:
最近几天研究ListView实现分页加载和滚动加载,发现可以用listView的OnScroll方法来实现,直接上代码
ListViewScroll.java
package zy.lucifer.ListViewScroll;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout.LayoutParams;
public class ListViewScroll extends Activity {
/** Called when the activity is first created. */
private LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
/**
* 设置布局显示目标最大化属性
*/
private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
ListView listView ;
private int lastItem = 0;
LinearLayout loadingLayout;
private listViewAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.myList);
Log.i("test", "onCreate(Bundle savedInstanceState)>>>>>>>>>>>>>>>");
// 线性布局
LinearLayout layout = new LinearLayout(this);
// 设置布局 水平方向
layout.setOrientation(LinearLayout.HORIZONTAL);
// 进度条
ProgressBar progressBar = new ProgressBar(this);
// 进度条显示位置
progressBar.setPadding(0, 0, 15, 0);
// 把进度条加入到layout中
layout.addView(progressBar, mLayoutParams);
// 文本内容
TextView textView = new TextView(this);
textView.setText("加载中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
// 把文本加入到layout中
layout.addView(textView, FFlayoutParams);
// 设置layout的重力方向,即对齐方式是
layout.setGravity(Gravity.CENTER);
// 设置ListView的页脚layout
loadingLayout = new LinearLayout(this);
loadingLayout.addView(layout, mLayoutParams);
loadingLayout.setGravity(Gravity.CENTER);
listView.addFooterView(loadingLayout);
adapter = new listViewAdapter();
listView.setAdapter(adapter);
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
Log.i("test" , "Scroll>>>first: " + firstVisibleItem + ", visible: " + visibleItemCount + ", total: " + totalItemCount);
lastItem = firstVisibleItem + visibleItemCount - 1;
Log.i("test" , "Scroll>>>lastItem:" + lastItem);
//显示50条ListItem,即0-49,因为onScroll是在“滑动”执行过之后才触发,所以用adapter.count<=41作条件
int scrolllength=101;
if (adapter.count<=scrolllength) {
if (firstVisibleItem+visibleItemCount==totalItemCount) {
adapter.count += 10;
adapter.notifyDataSetChanged();
listView.setSelection(lastItem);
int currentPage=adapter.count/10;
Toast.makeText(getApplicationContext(), "第"+currentPage+"页", Toast.LENGTH_LONG).show();
}
}
else {
listView.removeFooterView(loadingLayout);
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
});
}
class listViewAdapter extends BaseAdapter {
int count = 10; /* starting amount */
public int getCount() {
return count;
}
public Object getItem(int pos) {
return pos;
}
public long getItemId(int pos) {
return pos;
}
public View getView(int pos, View v, ViewGroup p) {
Log.i("test", "getView>>>pos:" + pos);
TextView view;
if (v == null) {
view = new TextView(ListViewScroll.this);
} else {
view = (TextView) v;
}
view.setText("ListItem " + pos);
view.setTextSize(20f);
view.setGravity(Gravity.CENTER);
view.setHeight(60);
return view;
}
}
}
main.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:cacheColorHint="#00000000" android:id="@+id/myList"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
</ListView>
</LinearLayout>
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:Android中ListView分页加载数据功能实现Android之ListView分页加载数据功能实现代码Android程序开发之Listview下拉刷新上拉(滑动分页)加载更多Android实现listview动态加载数据分页的两种方法Android应用中ListView利用OnScrollListener分页加载数据Android中ListView如何分页加载数据Android实现ListView分页自动加载数据的方法Android编程实现分页加载ListView功能示例