Created
December 1, 2015 14:14
-
-
Save hadifar/31986785564a1db21cab to your computer and use it in GitHub Desktop.
This is pagingListview for SO question
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
package com.paging.listview; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.AbsListView; | |
import android.widget.HeaderViewListAdapter; | |
import android.widget.ListAdapter; | |
import android.widget.ListView; | |
import java.util.List; | |
public class PagingListView extends ListView { | |
public interface Pagingable { | |
void onLoadMoreItems(); | |
} | |
private boolean isLoading; | |
private boolean hasMoreItems; | |
private Pagingable pagingableListener; | |
private OnScrollListener onScrollListener; | |
public PagingListView(Context context) { | |
super(context); | |
init(); | |
} | |
public PagingListView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public PagingListView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
} | |
public boolean isLoading() { | |
return this.isLoading; | |
} | |
public void setIsLoading(boolean isLoading) { | |
this.isLoading = isLoading; | |
} | |
public void setPagingableListener(Pagingable pagingableListener) { | |
this.pagingableListener = pagingableListener; | |
} | |
public void setHasMoreItems(boolean hasMoreItems) { | |
this.hasMoreItems = hasMoreItems; | |
// if(!this.hasMoreItems) { | |
// removeFooterView(loadingView); | |
// } | |
// else if(findViewById(R.id.loading_view) == null){ | |
// addFooterView(loadingView); | |
// ListAdapter listAdapter = ((HeaderViewListAdapter)getAdapter()).getWrappedAdapter(); | |
// setAdapter(listAdapter); | |
// } | |
} | |
public boolean hasMoreItems() { | |
return this.hasMoreItems; | |
} | |
public void onFinishLoading(boolean hasMoreItems, List<? extends Object> newItems) { | |
setHasMoreItems(hasMoreItems); | |
setIsLoading(false); | |
if (newItems != null && newItems.size() > 0) { | |
ListAdapter adapter = getAdapter(); | |
// ListAdapter listAdapter = ((HeaderViewListAdapter)getAdapter()).getWrappedAdapter(); | |
if (adapter instanceof PagingBaseAdapter) { | |
((PagingBaseAdapter)adapter).addMoreItems(newItems); | |
} else{ | |
try { | |
((PagingBaseAdapter) ((HeaderViewListAdapter) getAdapter()).getWrappedAdapter()).addMoreItems(newItems); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
// public void setLoadingView(View loadingView) { | |
// removeFooterView(this.loadingView); | |
// addFooterView(loadingView); | |
// this.loadingView = loadingView; | |
// } | |
private void init() { | |
isLoading = false; | |
// if(loadingView == null) | |
// loadingView = new LoadingView(getContext()); | |
super.setOnScrollListener(new OnScrollListener() { | |
@Override | |
public void onScrollStateChanged(AbsListView view, int scrollState) { | |
//Dispatch to child OnScrollListener | |
if (onScrollListener != null) { | |
onScrollListener.onScrollStateChanged(view, scrollState); | |
} | |
} | |
@Override | |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | |
//Dispatch to child OnScrollListener | |
if (onScrollListener != null) { | |
onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount); | |
} | |
int lastVisibleItem = firstVisibleItem + visibleItemCount; | |
if (!isLoading && hasMoreItems && (lastVisibleItem == totalItemCount)) { | |
if (pagingableListener != null) { | |
isLoading = true; | |
pagingableListener.onLoadMoreItems(); | |
} | |
} | |
} | |
}); | |
} | |
@Override | |
public void setOnScrollListener(OnScrollListener listener) { | |
onScrollListener = listener; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment