Forked from ssinss/EndlessRecyclerOnScrollListener.java
Last active
August 13, 2018 03:32
-
-
Save zfdang/38ae655a4fc401c99789 to your computer and use it in GitHub Desktop.
RecyclerView Supporting Pull Down to Refresh & Pull Up to Load More
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
<android.support.v4.widget.SwipeRefreshLayout | |
android:id="@+id/swipeRefreshLayout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/board_topic_list" | |
android:name="com.zfdang.zsmth_android.TopicListFragment" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginLeft="0dp" | |
android:layout_marginRight="0dp" | |
app:layoutManager="LinearLayoutManager" | |
android:scrollbars="vertical" | |
tools:context=".BoardTopicActivity" | |
tools:listitem="@layout/board_topic_item"/> | |
</android.support.v4.widget.SwipeRefreshLayout> |
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
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener { | |
public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName(); | |
private int previousTotal = 0; // The total number of items in the dataset after the last load | |
private boolean loading = false; // True if we are still waiting for the last set of data to load. | |
private int visibleThreshold = 2; // The minimum amount of items to have below your current scroll position before loading more. | |
int firstVisibleItem, visibleItemCount, totalItemCount; | |
private int current_page = 1; | |
private LinearLayoutManager mLinearLayoutManager; | |
public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) { | |
this.mLinearLayoutManager = linearLayoutManager; | |
} | |
@Override | |
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
if(dy < 0) { | |
return; | |
} | |
// check for scroll down only | |
visibleItemCount = recyclerView.getChildCount(); | |
totalItemCount = mLinearLayoutManager.getItemCount(); | |
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition(); | |
// to make sure only one onLoadMore is triggered | |
synchronized (this) { | |
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { | |
// End has been reached, Do something | |
current_page++; | |
onLoadMore(current_page); | |
loading = true; | |
} | |
} | |
} | |
public void setLoading(boolean loading) { | |
this.loading = loading; | |
} | |
public abstract void onLoadMore(int current_page); | |
} |
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
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
public class SampleActivity extends ActionBarActivity { | |
private EndlessRecyclerOnScrollListener mScrollListener = null; | |
private SwipeRefreshLayout mSwipeRefreshLayout = null; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_sample); | |
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list); | |
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); | |
recyclerView.setLayoutManager(linearLayoutManager); | |
// enable pull up for endless loading | |
mScrollListener = new EndlessRecyclerOnScrollListener(linearLayoutManager) { | |
@Override | |
public void onLoadMore(int current_page) { | |
// do something... | |
// after loading is done, please call the following method to re-enable onLoadMore | |
// usually it should be called in onCompleted() method | |
mScrollListener.setLoading(false); | |
} | |
}; | |
recyclerView.addOnScrollListener(mScrollListener); | |
// enable pull down to refresh | |
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); | |
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | |
@Override | |
public void onRefresh() { | |
// do something | |
// after refresh is done, remember to call the following code | |
if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) { | |
mSwipeRefreshLayout.setRefreshing(false); // This hides the spinner | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
one working sample is: https://github.com/zfdang/zSMTH-Android/blob/master/app/src/main/java/com/zfdang/zsmth_android/BoardTopicActivity.java