CustomListView
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import static android.content.Context.LAYOUT_INFLATER_SERVICE;
public class CustomListView extends ListView implements AbsListView.OnScrollListener {
private Context mContext;
//The Load more footer.
private View loadMoreFooter;
private boolean isLoadingMore;
private int currentScrollState;
private int currentFirstVisibleItem;
private int currentVisibleItemCount;
private int currentTotalItemCount;
private int currentLastItem;
public interface ListViewListener {
// Load data.
void loadData();
void onScrollEnd();
}
private ListViewListener mLoadDataListener;
/*** Instantiates a new Custom list view. *
* @param context the context
* @param attrs the attrs
* @param defStyle the def style */
public CustomListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setOnScrollListener(this);
mContext = context;
initializeLoadMore();
}
/*** Instantiates a new Custom list view.
* @param context the context
* @param attrs the attrs */
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnScrollListener(this);
mContext = context;
initializeLoadMore();
}
/*** Set load more listener.
* @param loadData the load data */
public void setLoadMoreListener(ListViewListener loadData){
mLoadDataListener =loadData;
}
/*** Instantiates a new Custom list view.
* @param context the context */
public CustomListView(Context context) {
super(context);
this.setOnScrollListener(this);
initializeLoadMore();
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.currentScrollState = scrollState;
this.isScrollCompleted();
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE&&mLoadDataListener!=null) {
mLoadDataListener.onScrollEnd();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.currentTotalItemCount = totalItemCount;
}
private void isScrollCompleted() {
if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
this.currentLastItem = this.currentFirstVisibleItem + this.currentVisibleItemCount;
if (currentLastItem == currentTotalItemCount && !(isLoadingMore)&&mLoadDataListener!=null) {
isLoadingMore = true;
addFooterView(loadMoreFooter);
loadMoreFooter.setVisibility(View.VISIBLE);
mLoadDataListener.loadData();
}
}
}
/*** Reset load more view. */
public void resetLoadMoreView() {
isLoadingMore = false;
loadMoreFooter.setVisibility(View.GONE);
removeFooterView(loadMoreFooter);
}
private void initializeLoadMore() {
loadMoreFooter = ((LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.loading_more_, null, false);
loadMoreFooter.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
ProgressBar progressBar = (ProgressBar) loadMoreFooter.findViewById(R.id.progressBar);
progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(mContext, R.color.login_bg),
android.graphics.PorterDuff.Mode.SRC_IN);
}
}
Adding a FooterView to the ListView (loading_more_.xml)
A footer View is nothing more than a piece of XML that defines how the footer of your listview will look like. Mine is as follows:
loading_more.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal"
android:padding="@dimen/padding_ten">
<ProgressBar android:id="@+id/progressBar"
android:layout_width="@dimen/height_width_20"
android:layout_height="@dimen/height_width_20"
/>
</LinearLayout>
At the end ,just implement ListViewListener in Activity or Fragment and set listener
listView.setLoadMoreListener(this); , you get lord more and scroll end event of list.