Android Tutorial(2)Endless Pagination for ListView

本文介绍如何在Android应用中实现ListView的无限滚动加载效果。通过监听ListView的滚动状态,在滚动到底部时自动加载更多数据。文章提供了具体的代码示例,包括自定义适配器和在Activity中实现的数据加载逻辑。
Android Tutorial(2)Endless Pagination for ListView

We have 2 options to load more information in ListView.
1. Add a button or link to the footer of the view, when we press it the system will load more content.
2. Check if the last item is visible and load more content when it is.

First of all, I plan to Viewing Android Source Code in Eclipse. That will help me a lot to study the codes.

1. Get the Android Source Code Viewing
After API Level 14, we have this 'Sources for Android SDK'. But for the previous versions, we need to install a plugin named "Android Sources".
https://code.google.com/p/adt-addons/

I install the plugin with URL
http://adt-addons.googlecode.com/svn/trunk/installer/com.android.ide.eclipse.installer.update/

But got Error Message:
Cannot complete the install because one or more required items could not be found.
Software being installed: Android SDK Installer 0.9.5.201012121703 (com.android.ide.eclipse.installer.feature.feature.group 0.9.5.201012121703)
Missing requirement: Android SDK Installer 0.9.5.201012121703 (com.android.ide.eclipse.installer.feature.feature.group 0.9.5.201012121703) requires 'org.eclipse.gef.all.feature.group 0.0.0' but it could not be found

Solution:
Change to root user and use the latest link:
http://adt-addons.googlecode.com/svn/trunk/source/com.android.ide.eclipse.source.update/

After that I got the source codes of android.

But I need to just use the latest package, do not use root user. That will cause problem if I build from maven. NO root User.

2. Make the Pagination ListView

Some times I got this Error Message when I start my emulator from snapshot.
Starting emulator for AVD 'CARL'
emulator: ERROR: Unable to load VM from snapshot. The snapshot has been saved for a different hardware configuration.

Solution:
Android Virtual Device Manager -> delete AVD -> new AVD, that solved the problem.

Change the adapter not extends from BaseAdapter, but from ArrayAdapter.

package com.sillycat.easyrestclientandroid.adapter;

import java.util.List;

import android.content.Context;
import android.widget.ArrayAdapter;

public abstract class AbstractBaseItemListAdapter<T> extends ArrayAdapter<T> {

public AbstractBaseItemListAdapter(Context context, int textViewResourceId,
List<T> objects) {
super(context, textViewResourceId, objects);
}

}

Almost no changes in the Adapter implementation.
public class ProductsListAdapter extends AbstractBaseItemListAdapter<Product> {


privatefinal LayoutInflater _layoutInflater;


public ProductsListAdapter(Context context, int textViewResourceId,List<Product> objects) {
super(context, textViewResourceId, objects);
this._layoutInflater = LayoutInflater.from(context);
}
…snip…


Most of the changes will happen in the activity.
package com.sillycat.easyrestclientandroid.activity.impl;

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

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import com.sillycat.easyrestclientandroid.activity.AbstractAsyncListActivity;
import com.sillycat.easyrestclientandroid.adapter.impl.ProductsListAdapter;
import com.sillycat.easyrestclientandroid.dao.ProductDAO;
import com.sillycat.easyrestclientandroid.dao.mock.ProductMockDAOImpl;
import com.sillycat.easyrestclientandroid.model.Product;

public class ProductsListActivity extends AbstractAsyncListActivity {

protected static final String TAG = ProductsListActivity.class
.getSimpleName();

int pageSize = 5;

int currentPage = 1;

boolean loadingMore = false;

ProductsListAdapter adapter;

List<Product> items;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

items = new ArrayList<Product>();

adapter = new ProductsListAdapter(this,
android.R.layout.simple_list_item_1, items);

setListAdapter(adapter);

}

public void onStart() {
super.onStart();
new DownloadStatesTask().execute(currentPage);
}

public void refreshStates(List<Product> items) {

if (items == null || items.isEmpty()) {
return;
}

for (int i = 0; i < items.size(); i++) {
adapter.add(items.get(i));
}
setTitle("Products List with " + String.valueOf(adapter.getCount())
+ " items");

adapter.notifyDataSetChanged();

this.getListView().setOnScrollListener(new OnScrollListener() {

public void onScrollStateChanged(AbsListView view, int scrollState) {
}

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

int lastInScreen = firstVisibleItem + visibleItemCount;

Log.d(TAG, "firstVisibleItem = " + firstVisibleItem + " visibleItemCount = " + visibleItemCount + " totalItemCount = " + totalItemCount);
if ((lastInScreen == totalItemCount) && !(loadingMore)) {
currentPage = currentPage + 1;
new DownloadStatesTask().execute(currentPage);
}
}
});
loadingMore = false;
}

private class DownloadStatesTask extends
AsyncTask<Integer, Void, List<Product>> {
@Override
protected void onPreExecute() {
loadingMore = true;
showLoadingProgressDialog();
}

@Override
protected List<Product> doInBackground(Integer... params) {
try {
Log.d(TAG, "Hitting the current page params = " + params[0]);
ProductDAO dao = new ProductMockDAOImpl();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return dao.pagination(params[0], pageSize);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}

@Override
protected void onPostExecute(List<Product> result) {
dismissProgressDialog();
refreshStates(result);
}
}

}


References:
http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/

Source Plugin
http://manski.net/2011/11/android-source-code-in-eclipse/
http://adt-addons.googlecode.com/svn/trunk/source/com.android.ide.eclipse.source.update/

Customer ArrayAdatper
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/
http://sogacity.com/how-to-make-a-custom-arrayadapter-for-listview/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值