效果图:
首先将布局写好
activity_main:
<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" >
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</RelativeLayout>
item_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="24sp"
android:text="第一项"/>
</LinearLayout>
接下来写好适配器
MyAdapter:
package com.example.jhl.jhllearn;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<String> oList;
private LayoutInflater inflater;
public MyAdapter(Context context,List<String> oList) {
this.context=context;
this.oList=oList;
this.inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return oList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return oList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
if (convertView==null) {
holder=new ViewHolder();
convertView=inflater.inflate(R.layout.item_layout, null);
holder.tv_item=(TextView)convertView.findViewById(R.id.tv_item);
convertView.setTag(holder);
}else
{
holder=(ViewHolder)convertView.getTag();
}
holder.tv_item.setText(oList.get(position));
return convertView;
}
public class ViewHolder{
TextView tv_item;
}
}
进入主代码:
MainActivity:
package com.example.jhl.jhllearn;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity implements Thread.UncaughtExceptionHandler
{
private ListView lv_list;
// 页数
private int pager = 1;
// 总数据
private List<String> oList = new ArrayList<String>();
// 当前数据集合
private List<String> single_list = new ArrayList<String>();
// 用于判断是否还能分页
private boolean isMore = false;
// 适配器
private MyAdapter adapter;
private LinearLayout ll_more;
private ProgressBar pBar;
private TextView tv_more;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread.setDefaultUncaughtExceptionHandler(this);
lv_list = (ListView) this.findViewById(R.id.lv_list);
init_date();
load_foot();
init_list();
lv_list.setOnScrollListener(scrollListener);
}
/**
* 添加总数据
*/
private void init_date()
{
for (int i = 0; i < 46; i++)
{
oList.add("当前为第" + (i + 1) + "条数据信息");
}
}
/**
* 将布局加载至ListView底部
*/
public void load_foot(){
// 加载分页提示信息布局,加载至listview的底部
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.pro_layout, null);
ll_more = (LinearLayout) view.findViewById(R.id.ll_more);
pBar = (ProgressBar) view.findViewById(R.id.progress);
tv_more = (TextView) view.findViewById(R.id.tv_more);
ll_more.setVisibility(View.GONE);
pBar.setVisibility(View.GONE);
// 加布局添加至listview底部
lv_list.addFooterView(view);
}
// 用于统计当前列表上所显示的总的项目
private int count = 0;
private OnScrollListener scrollListener = new OnScrollListener()
{
// OnScrollListener.SCROLL_STATE_FLING; //屏幕处于甩动状态
// OnScrollListener.SCROLL_STATE_IDLE; //停止滑动状态
// OnScrollListener.SCROLL_STATE_TOUCH_SCROLL;// 手指接触状态
// 记录当前滑动状态
@Override
public void onScrollStateChanged(AbsListView view, int scrollListener)
{
// 当列表开始滚动,停止滚动,正在滑动的时候进行回调
if (isMore && count == adapter.getCount()
&& scrollListener == OnScrollListener.SCROLL_STATE_IDLE)
{
thread_page();
init_page();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount)
{
// 当列表正在滚动的时候,一直会执行
count = firstVisibleItem + visibleItemCount - 1;
}
};
private void thread_page()
{
new Thread()
{
public void run()
{
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0x123);
};
}.start();
}
private Handler handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
if (msg.what == 0x123)
{
init_list();
if (isMore)
{
pBar.setVisibility(View.GONE);
tv_more.setText("更多内容。。。");
} else
{
pBar.setVisibility(View.GONE);
tv_more.setText("总共" + single_list.size() + "项");
}
}
};
};
/*
* 进行分页自增
*/
public void init_page()
{
if (isMore) {
ll_more.setVisibility(View.VISIBLE);
pBar.setVisibility(View.VISIBLE);
tv_more.setText("正在加载数据....");
pager++;
}
}
/**
* 实现分页加载
*/
private void init_list()
{
List<String> infos = new ArrayList<String>();
// 通过总的数据减去已加载的数据,判断未加载的数据是否大于10条
if (oList.size() - single_list.size() < 10)
{
infos = oList.subList((pager-1) * 10, oList.size());
} else
{ // (0,10)
infos = oList.subList((pager-1) * 10, 10 * pager);
}
if (infos.size()<10){
isMore=false;
}else{
isMore=true;
}
// 将分页加载的数据添加至当前集合
single_list.addAll(infos);
if (adapter == null)
{
adapter = new MyAdapter(this, single_list);
lv_list.setAdapter(adapter);
} else
{
// 刷新适配器
adapter.notifyDataSetChanged();
}
}
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
Log.i("MainActivity",throwable.getMessage());
}
}
end,
不会的小伙伴赶快敲起来吧。