Created
June 27, 2016 14:50
-
-
Save Domacoski/aa0fb361629b5af3765668fad3345e49 to your computer and use it in GitHub Desktop.
EndlessScrollListener
This file contains 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.widget.AbsListView; | |
/** | |
* Classe controla a atualização da Lista conforme a mesma chega ao fim | |
*/ | |
public class EndlessScrollListener implements AbsListView.OnScrollListener{ | |
// private int visibleThreshold = ; | |
private int currentPage = -1; // incia em 0 | |
private int currentTotalItens = 0; | |
// private int firstItemPageIndex = 0; | |
private boolean loading = false; | |
private boolean nextPage = true; | |
private OnLoadMoreListener onLoadMoreListener; | |
public EndlessScrollListener(final OnLoadMoreListener onLoadMoreListener) | |
{ | |
this.onLoadMoreListener = onLoadMoreListener; | |
} | |
@Override | |
public void onScrollStateChanged(AbsListView view, int scrollState) | |
{ | |
} | |
@Override | |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) | |
{ | |
if(!nextPage) | |
return; | |
if(totalItemCount < currentTotalItens) | |
{ | |
this.currentPage = 0; | |
this.currentTotalItens = totalItemCount; | |
if( totalItemCount == 0 ){ this.loading = true; } | |
} | |
if( loading && (totalItemCount > currentTotalItens) ) | |
{ | |
loading = false; | |
currentTotalItens = totalItemCount; | |
currentPage++; | |
} | |
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + 3)) | |
{ | |
onLoadMoreListener.onLoadMore(currentPage+1, totalItemCount); | |
loading = true; | |
} | |
} | |
public void setNextPage(boolean nextPage) | |
{ | |
this.nextPage = nextPage; | |
} | |
public interface OnLoadMoreListener { | |
void onLoadMore(int page, int totalItemsCount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment