Skip to content

Instantly share code, notes, and snippets.

@futtetennista
Created April 2, 2012 08:12
Show Gist options
  • Save futtetennista/2281552 to your computer and use it in GitHub Desktop.
Save futtetennista/2281552 to your computer and use it in GitHub Desktop.
EndlessPager
private class EndlessOnPageChangeListener implements OnPageChangeListener {
private static final int NO_PAGE = -1;
int loadingPage = NO_PAGE;
@Override
public void onPageSelected(int currentPage) {
// we're not on the loading element
if (loadingPage != currentPage) {
loadingPage = NO_PAGE;
currentAdapter.setLoadingData(false);
if (!currentAdapter.isFooterPosition(currentPage)) {
// create footer view
} else {
// create default view
}
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int nextPosition = position + 1;
// set loading data if it's not the last page (where lastPage() is a method defined somewhere)
if (nextPosition == currentAdapter.getCount() && !lastPage()) {
loadingPage = nextPosition;
currentAdapter.setLoadingData(true);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
public void setLoadingPage(int loadingPage) {
this.loadingPage = loadingPage;
}
}
import java.util.ArrayList;
import java.util.List;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
public abstract class EndlessPagerAdapter<ItemT> extends PagerAdapter {
private boolean isLoadingData;
protected ArrayList<ItemT> data = new ArrayList<ItemT>();
protected View progressView;
/**
* Create the page for the given position. The adapter is responsible for adding the view to the
* container given here, although it only must ensure this is done by the time it returns from
* {@link #finishUpdate()}.
*
* @param container
* The containing View in which the page will be shown.
* @param position
* The page position to be instantiated.
* @return Returns an Object representing the new page. This does not need to be a View, but can
* be some other container of the page.
*/
@Override
public Object instantiateItem(View container, int position) {
if (isPositionOfProgressElement(position)) {
return doInstatiateProgressItem(container);
}
return doInstantiateItem(container, position);
}
protected abstract Object doInstantiateItem(View container, int position);
protected abstract Object doInstatiateProgressItem(View container);
/**
* Remove a page for the given position. The adapter is responsible for removing the view from
* its container, although it only must ensure this is done by the time it returns from
* {@link #finishUpdate()}.
*
* @param container
* The containing View from which the page will be removed.
* @param position
* The page position to be removed.
* @param object
* The same object that was returned by {@link #instantiateItem(View, int)}.
*/
@Override
public void destroyItem(View container, int position, Object view) {
((ViewPager) container).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
/**
* Called when the a change in the shown pages has been completed. At this point you must ensure
* that all of the pages have actually been added or removed from the container as appropriate.
*
* @param container
* The containing View which is displaying this adapter's page views.
*/
@Override
public void finishUpdate(View container) {
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View container) {
}
public void setLoadingData(boolean isLoadingData) {
this.isLoadingData = isLoadingData;
}
public boolean isLoadingData() {
return isLoadingData;
}
public boolean isPositionOfProgressElement(int position) {
return isLoadingData && position == data.size();
}
@Override
public int getItemPosition(Object object) {
if (object.equals(progressView) && !isLoadingData) {
return PagerAdapter.POSITION_NONE;
}
return PagerAdapter.POSITION_UNCHANGED;
}
@Override
public int getCount() {
int size = 0;
if (data != null) {
size += data.size();
}
if (isLoadingData) {
size += 1;
}
return size;
}
public void addAll(List<ItemT> list) {
addAll(list, true);
}
public void addAll(List<ItemT> list, boolean redrawPager) {
data.addAll(list);
if (redrawPager) {
notifyDataSetChanged();
}
}
public void add(ItemT item) {
add(item, true);
}
public void add(ItemT item, boolean redrawPager) {
data.add(item);
if (redrawPager) {
notifyDataSetChanged();
}
}
public void add(int index, ItemT item) {
add(index, item, true);
}
public void add(int index, ItemT item, boolean redrawPager) {
data.add(index, item);
if (redrawPager) {
notifyDataSetChanged();
}
}
public void addAll(int index, List<ItemT> list) {
addAll(index, list, true);
}
public void addAll(int index, List<ItemT> list, boolean redrawPager) {
data.addAll(index, list);
if (redrawPager) {
notifyDataSetChanged();
}
}
public void clear(ViewPager pager) {
for (int i = 0; i < getCount(); i++) {
destroyItem(pager, i, pager.getChildAt(i));
}
data.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment