1. LoadMoreListView.class
public class LoadMoreListView extends ListView implements OnScrollListener {
private static final String TAG = "LoadMoreListView";
/**
* Listener that will receive notifications every time the list scrolls.
*/
private OnScrollListener onScrollListener;
private LayoutInflater inflater;
// footer view
private RelativeLayout footerView;
// private TextView mLabLoadMore;
private ProgressBar progressBarLoadMore;
// Listener to process load more items when user reaches the end of the list
private OnLoadMoreListener onLoadMoreListener;
// To know if the list is loading more items
private boolean isLoadingMore = false;
private int currentScrollState;
public LoadMoreListView(Context context) {
super(context);
init(context);
}
public LoadMoreListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// footer
footerView = (RelativeLayout) inflater.inflate(R.layout.load_more_footer, this, false);
/*
* mLabLoadMore = (TextView) mFooterView .findViewById(R.id.load_more_lab_view);
*/
progressBarLoadMore = (ProgressBar) footerView.findViewById(R.id.load_more_progressBar);
addFooterView(footerView);
super.setOnScrollListener(this);
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
}
/**
* Set the listener that will receive notifications every time the list scrolls.
*
* @param l The scroll listener.
*/
@Override
public void setOnScrollListener(AbsListView.OnScrollListener l) {
onScrollListener = l;
}
/**
* Register a callback to be invoked when this list reaches the end (last item be visible)
*
* @param onLoadMoreListener The callback to run.
*/
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (onScrollListener != null) {
onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
if (onLoadMoreListener != null) {
if (visibleItemCount == totalItemCount) {
progressBarLoadMore.setVisibility(View.GONE);
// mLabLoadMore.setVisibility(View.GONE);
return;
}
boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;
if (!isLoadingMore && loadMore && currentScrollState != SCROLL_STATE_IDLE) {
progressBarLoadMore.setVisibility(View.VISIBLE);
// mLabLoadMore.setVisibility(View.VISIBLE);
isLoadingMore = true;
onLoadMore();
}
}
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
// bug fix: listview was not clickable after scroll
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
view.invalidateViews();
}
currentScrollState = scrollState;
if (onScrollListener != null) {
onScrollListener.onScrollStateChanged(view, scrollState);
}
}
public void onLoadMore() {
Log.d(TAG, "onLoadMore");
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
}
/**
* Notify the loading more operation has finished
*/
public void onLoadMoreComplete() {
isLoadingMore = false;
progressBarLoadMore.setVisibility(View.GONE);
}
/**
* Interface definition for a callback to be invoked when list reaches the last item (the user
* load more items in the list)
*/
public interface OnLoadMoreListener {
/**
* Called when the list reaches the last item (the last item is visible to the user)
*/
public void onLoadMore();
}
}
2. Activity to LoadMoreListView
// set a listener to be invoked when the list reaches the end ((LoadMoreListView) getListView()) .setOnLoadMoreListener(new OnLoadMoreListener() { public void onLoadMore() { // Do the work to load more items at the end of list here new LoadDataTask().execute(); } }); private class LoadDataTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { if (isCancelled()) { return null; } // Simulates a background task try { Thread.sleep(1000); } catch (InterruptedException e) { } for (int i = 0; i < mNames.length; i++) mListItems.add(mNames[i]); return null; } @Override protected void onPostExecute(Void result) { mListItems.add("Added after load more"); // We need notify the adapter that the data have been changed ((BaseAdapter) getListAdapter()).notifyDataSetChanged(); // Call onLoadMoreComplete when the LoadMore task, has finished ((LoadMoreListView) getListView()).onLoadMoreComplete(); super.onPostExecute(result); } @Override protected void onCancelled() { // Notify the loading more operation has finished ((LoadMoreListView) getListView()).onLoadMoreComplete(); } }
3. Layout
<!-- We have to indicate that the listview is now a LoadMoreListView --> <com.costum.android.widget.LoadMoreListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" />
참고
https://github.com/shontauro/android-pulltorefresh-and-loadmore
android-pulltorefresh-and-loadmore-master.zip
'프로그래밍 > Android' 카테고리의 다른 글
Android 비트맵의 라운드 처리 (0) | 2015.02.17 |
---|---|
Android intent ACTION_VIEW valid url check (0) | 2015.02.17 |
Android ExpandableListView (0) | 2015.02.05 |
Android create keyhash from debug.keystore and release.keystore (0) | 2015.01.30 |
Android create keyhash (0) | 2015.01.26 |
댓글