Skip to content

Instantly share code, notes, and snippets.

@rishabhmhjn
Last active December 18, 2017 08:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rishabhmhjn/79ce88d5abf6c404603c to your computer and use it in GitHub Desktop.
Save rishabhmhjn/79ce88d5abf6c404603c to your computer and use it in GitHub Desktop.
Custom ListView for use case such as endless scrolling
<?xml version="1.0" encoding="utf-8"?>
<me.rishabhmhjn.widgets.LoadMoreListView
android:id="+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
package me.rishabhmhjn.widgets;
import me.rishabhmhjn.R; // might be diffrent for your use
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
public class LoadMoreListView extends ListView implements OnScrollListener {
private static final String TAG = LoadMoreListView.class
.getSimpleName();
private OnScrollListener mOnScrollListener;
private LayoutInflater mInflater;
private View mFooterView;
private View mLoadMoreStatusView;
private OnLoadMoreListener mOnLoadMoreListener;
private boolean mIsLoadingMore = false;
private int mCurrentScrollState;
public LoadMoreListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
@Override
public void setAdapter(ListAdapter adapter) {
mFooterView = (RelativeLayout) mInflater.inflate(R.layout.load_more_footer,
this, false);
mLoadMoreStatusView = mFooterView.findViewById(R.id.load_more_progress_bar);
addFooterView(mFooterView);
setLoading(false);
super.setAdapter(adapter);
}
private void init(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
super.setOnScrollListener(this);
}
public void setLoadMoreStatusView(View v, int statusViewId) {
removeFooterView(mFooterView);
mFooterView = v;
mLoadMoreStatusView = mFooterView.findViewById(statusViewId);
addFooterView(mFooterView);
}
public void setLoadMoreStatusView(View v) {
removeFooterView(mFooterView);
mFooterView = v;
mLoadMoreStatusView = mFooterView.findViewById(R.id.load_more_progress_bar);
addFooterView(mFooterView);
}
/**
* Set the listener that will receive notifications every time the list
* scrolls.
*
* @param l
* The scroll listener.
*/
@Override
public void setOnScrollListener(AbsListView.OnScrollListener l) {
mOnScrollListener = l;
}
/**
* Register a callback to be invoked when this list reaches the end (last item
* be visible)
*
* @param onLoadMoreListener
* The callback to run.
*/
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
mOnLoadMoreListener = onLoadMoreListener;
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (mOnScrollListener != null) {
mOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount,
totalItemCount);
}
if (visibleItemCount == totalItemCount) {
if (mLoadMoreStatusView != null) {
mLoadMoreStatusView.setVisibility(View.GONE);
}
return;
}
if (mOnLoadMoreListener != null) {
boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;
if (!mIsLoadingMore && loadMore
&& mCurrentScrollState != SCROLL_STATE_IDLE) {
setLoading(true);
onLoadMore();
}
}
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
mCurrentScrollState = scrollState;
if (mOnScrollListener != null) {
mOnScrollListener.onScrollStateChanged(view, scrollState);
}
}
public void setLoading(boolean loading) {
Log.d(TAG, "setLoading: " + loading);
mIsLoadingMore = loading;
mLoadMoreStatusView.setVisibility(loading ? View.VISIBLE : View.GONE);
}
public void onLoadMore() {
Log.d(TAG, "onLoadMore");
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
}
}
public void onLoadMoreComplete() {
setLoading(false);
}
public interface OnLoadMoreListener {
public void onLoadMore();
}
}
package me.rishabhmhjn;
import me.rishabhmhjn.widgets.LoadMoreListView;
import me.rishabhmhjn.widgets.LoadMoreListView.OnLoadMoreListener;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity {
private LoadMoreListView mLoadMoreListView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_activity);
mLoadMoreListView = findViewById(R.id.listview);
mLoadMoreListView.setOnLoadMoreListener(new OnLoadMoreListener() {
public void onLoadMore() {
// load more items or do other processing
// when completed, execute onLoadMoreComplete() of the listview
mLoadMoreListView.onLoadMoreComplete();
};
});
}
}
@hartviqb
Copy link

hartviqb commented Mar 9, 2016

Good widget, why you don't upload to gradle repository?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment