Last active
March 29, 2016 14:15
-
-
Save VAdaihiep/1c5c2122a08a4bfcccc2 to your computer and use it in GitHub Desktop.
Load more (paging) ListView in simplest way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.AbsListView; | |
import android.widget.BaseAdapter; | |
import android.widget.HeaderViewListAdapter; | |
import android.widget.ListView; | |
public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener { | |
private OnLoadMorListener onLoadMorListener; | |
private int mLastVisibleEnd; | |
private View mFooter; | |
private boolean hasFooter; | |
private boolean isLoadFinished; | |
public LoadMoreListView(Context context) { | |
super(context); | |
init(); | |
addFooter(); | |
} | |
public LoadMoreListView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
addFooter(); | |
} | |
public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
addFooter(); | |
} | |
private void init() { | |
setOnScrollListener(this); | |
hasFooter = false; | |
isLoadFinished = false; | |
mLastVisibleEnd = 0; | |
} | |
OnScrollListener onScrollListener; | |
public void addOnScrollListener(OnScrollListener onScrollListener) { | |
this.onScrollListener = onScrollListener; | |
} | |
@Override | |
public void onScrollStateChanged(AbsListView view, int scrollState) { | |
if (onScrollListener != null) { | |
onScrollListener.onScrollStateChanged(view, scrollState); | |
} | |
} | |
@Override | |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | |
if (onScrollListener != null) { | |
onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount); | |
} | |
int lastItem = firstVisibleItem + visibleItemCount; | |
if (lastItem == totalItemCount) { | |
if (lastItem != mLastVisibleEnd && totalItemCount > 1) { | |
mLastVisibleEnd = lastItem; | |
triggerLastVisible(); | |
} | |
} | |
} | |
private void triggerLastVisible() { | |
MyLog.d("Last visible isLoadFinished = " + isLoadFinished); | |
if (isLoadFinished) return; | |
MyLog.d("Trigger load more"); | |
if (onLoadMorListener != null) { | |
onLoadMorListener.loadmore(this, (BaseAdapter) ((HeaderViewListAdapter) getAdapter()).getWrappedAdapter()); | |
} | |
} | |
public void setLoadMoreView(View view) { | |
if (mFooter != null) removeFooterView(mFooter); | |
mFooter = view; | |
} | |
public void setOnLoadMoreListener(OnLoadMorListener refreshListener) { | |
onLoadMorListener = refreshListener; | |
} | |
public void setLoadFinished(boolean loadFinished) { | |
isLoadFinished = loadFinished; | |
if (isLoadFinished) { | |
removeFooter(); | |
} else if(!hasFooter) { | |
addFooter(); | |
} | |
} | |
private void addFooter() { | |
if (mFooter == null) { | |
mFooter = View.inflate(getContext(), R.layout.item_loading_more, null); | |
} | |
addFooterView(mFooter); | |
hasFooter = true; | |
} | |
private void removeFooter() { | |
if (mFooter == null) return; | |
mLastVisibleEnd--; | |
removeFooterView(mFooter); | |
hasFooter = false; | |
} | |
public interface OnLoadMorListener { | |
/** | |
* @param listView the listview itself | |
* @param adapter the adapter that you need to repopulate | |
*/ | |
void loadmore(LoadMoreListView listView, BaseAdapter adapter); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LoadMoreListView lstLoadMore = (LoadMoreListView) findViewById(R.id.listview); | |
lstLoadMore.setOnLoadMoreListener(new LoadMoreListView.OnLoadMorListener() { | |
@Override | |
public void loadmore(LoadMoreListView listView, BaseAdapter adapter) { | |
loadData(); | |
} | |
}); | |
/////////////////////////////////////////////////////////////////////////////// | |
// Check load all item -> call setLoadFinished to avoid load more | |
if(loadedAllItem){ | |
lstLoadMore.setLoadFinished(true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment