Monday, June 20, 2016

Android - Load more custom ListView with footer

Overview:- If you are loading data from a web server and the list is huge, then the practical solution would be to load a certain amount and if the user scrolls to the end of the list, load some more and keep going until you load the full list. 




I have handled the following events:


Add a view that will visible on the footer of a list when app loading data from the server.

Notify to your view when load more event occurs.

Notify to your view when scroll end.


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.


Share:

Get it on Google Play

React Native - Start Development with Typescript

React Native is a popular framework for building mobile apps for both Android and iOS. It allows developers to write JavaScript code that ca...