首页 > 代码库 > Android ListView加载更多

Android ListView加载更多

先看效果:

ListView的footer布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" >

    <TextView
        android:id="@+id/pulldown_footer_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="加载更多日志"
        android:textSize="15dp" />

    <ProgressBar
        android:id="@+id/pulldown_footer_loading"
        style="@android:style/Widget.ProgressBar.Small.Inverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"/>

</RelativeLayout>

ListView的item和main_activity布局很简单,分别是一个TextView和ListView,不再贴出。

MainActivity代码贴出:

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
	private ListView listview;
	private LayoutInflater inflater;
	private List<String> arrays = new ArrayList<String>();
	private RelativeLayout footer;
	private MyAdapter adapter;
	// 每页显示条数
	private Integer perpage = 20;
	// 当前页数
	private Integer currentpage = 1;
	// 最大页数
	private Integer maxpage = 1;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		setMaxPage(40);
		loadData(currentpage, perpage);
		
		inflater = getLayoutInflater();
		listview = (ListView) this.findViewById(R.id.pulldownview);
		footer = (RelativeLayout) View.inflate(getApplicationContext(), R.layout.pulldown_footer, null);
		listview.addFooterView(footer, null, true);
		adapter = new MyAdapter();
		listview.setAdapter(adapter);
		listview.setOnScrollListener(new OnScrollListener()
		{

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState)
			{
				if (view.getLastVisiblePosition() == view.getCount() - 1)
				{
					if (currentpage != maxpage)
					{
						currentpage++;
						loadData(currentpage, perpage);
						adapter.notifyDataSetChanged();
					} else
					{
						Toast.makeText(getApplicationContext(), "没有更多的数据!", Toast.LENGTH_SHORT).show();
						listview.removeFooterView(footer);
					}
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
			{

			}
		});
	}

	/**
	 * 模拟分页查询数据
	 * 
	 * @param currentpage当前页数
	 * @param perpage
	 *            每页显示数
	 */
	private void loadData(Integer currentpage, Integer perpage)
	{
		// 起始数据
		Integer firstResult = (currentpage - 1) * perpage;
		// 获取的最大数据
		Integer maxResult = currentpage * perpage;
		System.out.println("firstResult:"+firstResult+"  "+"maxResult:"+maxResult);
		// select * from database where XXXX order by XX limit firstResult,maxResult //order by可省略,但有order by时limit必需在它后面
		for (; firstResult < maxResult; firstResult++)
		{
			arrays.add("这是测试数据" + (firstResult+1));
		}
	}

	/**
	 * 模拟获取最大页数
	 * 
	 * @param total
	 *            总的数据
	 */
	private void setMaxPage(Integer total)
	{
		if (total % perpage == 0)
		{
			maxpage = total / perpage;
		} else
		{
			maxpage = total / perpage + 1;
		}
		System.out.println("maxpage:"+maxpage);
	}

	private class MyAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			return arrays == null ? 0 : arrays.size();
		}

		@Override
		public Object getItem(int position)
		{
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			ViewHolder holder;
			if (convertView == null)
			{
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.item_list, null);
				holder.textView = (TextView) convertView.findViewById(R.id.text);
				convertView.setTag(holder);
			} else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.textView.setText(arrays.get(position));
			return convertView;
		}
	}

	private static class ViewHolder
	{
		TextView textView;
	}
}