Skip to content

Instantly share code, notes, and snippets.

@imran0101
Last active February 22, 2022 12:50
Show Gist options
  • Save imran0101/b6d4b3d65b0b4775a22e to your computer and use it in GitHub Desktop.
Save imran0101/b6d4b3d65b0b4775a22e to your computer and use it in GitHub Desktop.

Revisions

  1. imran0101 revised this gist Jul 6, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions RecyclerViewPositionHelper.java
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,11 @@
    *
    * compile 'com.android.support:recyclerview-v7:22.0.0'
    */

    import android.support.v7.widget.OrientationHelper;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;

    public class RecyclerViewPositionHelper {

    final RecyclerView recyclerView;
  2. imran0101 revised this gist May 12, 2015. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions RecyclerViewPositionHelper.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    /**
    * RecyclerView position helper class for any LayoutManager.
    *
    * compile 'com.android.support:recyclerview-v7:22.0.0'
    */
    public class RecyclerViewPositionHelper {

    @@ -36,7 +38,7 @@ public int getItemCount() {
    */
    public int findFirstVisibleItemPosition() {
    final View child = findOneVisibleChild(0, layoutManager.getChildCount(), false, true);
    return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    /**
    @@ -48,7 +50,7 @@ public int findFirstVisibleItemPosition() {
    */
    public int findFirstCompletelyVisibleItemPosition() {
    final View child = findOneVisibleChild(0, layoutManager.getChildCount(), true, false);
    return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    /**
    @@ -60,7 +62,7 @@ public int findFirstCompletelyVisibleItemPosition() {
    */
    public int findLastVisibleItemPosition() {
    final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, false, true);
    return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    /**
    @@ -72,7 +74,7 @@ public int findLastVisibleItemPosition() {
    */
    public int findLastCompletelyVisibleItemPosition() {
    final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, true, false);
    return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
  3. imran0101 revised this gist May 3, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion EndlessRecyclerOnScrollListener.java
    Original file line number Diff line number Diff line change
    @@ -41,5 +41,5 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    }

    //Start loading
    public abstract void onLoadMore(int current_page);
    public abstract void onLoadMore(int currentPage);
    }
  4. imran0101 revised this gist May 3, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions EndlessRecyclerOnScrollListener.java
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScr
    private int visibleThreshold = 5; // 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 int currentPage = 1;

    RecyclerViewPositionHelper mRecyclerViewHelper;

    @@ -32,9 +32,9 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    <= (firstVisibleItem + visibleThreshold)) {
    // End has been reached
    // Do something
    current_page++;
    currentPage++;

    onLoadMore(current_page);
    onLoadMore(currentPage);

    loading = true;
    }
  5. imran0101 revised this gist May 3, 2015. 1 changed file with 109 additions and 0 deletions.
    109 changes: 109 additions & 0 deletions RecyclerViewPositionHelper.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    /**
    * RecyclerView position helper class for any LayoutManager.
    */
    public class RecyclerViewPositionHelper {

    final RecyclerView recyclerView;
    final RecyclerView.LayoutManager layoutManager;

    RecyclerViewPositionHelper(RecyclerView recyclerView) {
    this.recyclerView = recyclerView;
    this.layoutManager = recyclerView.getLayoutManager();
    }

    public static RecyclerViewPositionHelper createHelper(RecyclerView recyclerView) {
    if (recyclerView == null) {
    throw new NullPointerException("Recycler View is null");
    }
    return new RecyclerViewPositionHelper(recyclerView);
    }

    /**
    * Returns the adapter item count.
    *
    * @return The total number on items in a layout manager
    */
    public int getItemCount() {
    return layoutManager == null ? 0 : layoutManager.getItemCount();
    }

    /**
    * Returns the adapter position of the first visible view. This position does not include
    * adapter changes that were dispatched after the last layout pass.
    *
    * @return The adapter position of the first visible item or {@link RecyclerView#NO_POSITION} if
    * there aren't any visible items.
    */
    public int findFirstVisibleItemPosition() {
    final View child = findOneVisibleChild(0, layoutManager.getChildCount(), false, true);
    return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    /**
    * Returns the adapter position of the first fully visible view. This position does not include
    * adapter changes that were dispatched after the last layout pass.
    *
    * @return The adapter position of the first fully visible item or
    * {@link RecyclerView#NO_POSITION} if there aren't any visible items.
    */
    public int findFirstCompletelyVisibleItemPosition() {
    final View child = findOneVisibleChild(0, layoutManager.getChildCount(), true, false);
    return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    /**
    * Returns the adapter position of the last visible view. This position does not include
    * adapter changes that were dispatched after the last layout pass.
    *
    * @return The adapter position of the last visible view or {@link RecyclerView#NO_POSITION} if
    * there aren't any visible items
    */
    public int findLastVisibleItemPosition() {
    final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, false, true);
    return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    /**
    * Returns the adapter position of the last fully visible view. This position does not include
    * adapter changes that were dispatched after the last layout pass.
    *
    * @return The adapter position of the last fully visible view or
    * {@link RecyclerView#NO_POSITION} if there aren't any visible items.
    */
    public int findLastCompletelyVisibleItemPosition() {
    final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, true, false);
    return child == null ? NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
    boolean acceptPartiallyVisible) {
    OrientationHelper helper;
    if (layoutManager.canScrollVertically()) {
    helper = OrientationHelper.createVerticalHelper(layoutManager);
    } else {
    helper = OrientationHelper.createHorizontalHelper(layoutManager);
    }

    final int start = helper.getStartAfterPadding();
    final int end = helper.getEndAfterPadding();
    final int next = toIndex > fromIndex ? 1 : -1;
    View partiallyVisible = null;
    for (int i = fromIndex; i != toIndex; i += next) {
    final View child = layoutManager.getChildAt(i);
    final int childStart = helper.getDecoratedStart(child);
    final int childEnd = helper.getDecoratedEnd(child);
    if (childStart < end && childEnd > start) {
    if (completelyVisible) {
    if (childStart >= start && childEnd <= end) {
    return child;
    } else if (acceptPartiallyVisible && partiallyVisible == null) {
    partiallyVisible = child;
    }
    } else {
    return child;
    }
    }
    }
    return partiallyVisible;
    }
    }
  6. imran0101 created this gist May 3, 2015.
    45 changes: 45 additions & 0 deletions EndlessRecyclerOnScrollListener.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    /**
    * Custom Scroll listener for RecyclerView.
    * Based on implementation https://gist.github.com/ssinss/e06f12ef66c51252563e
    */
    public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
    public static String TAG = "EndlessScrollListener";

    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of data to load.
    private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
    int firstVisibleItem, visibleItemCount, totalItemCount;

    private int current_page = 1;

    RecyclerViewPositionHelper mRecyclerViewHelper;

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    mRecyclerViewHelper = RecyclerViewPositionHelper.createHelper(recyclerView);
    visibleItemCount = recyclerView.getChildCount();
    totalItemCount = mRecyclerViewHelper.getItemCount();
    firstVisibleItem = mRecyclerViewHelper.findFirstVisibleItemPosition();

    if (loading) {
    if (totalItemCount > previousTotal) {
    loading = false;
    previousTotal = totalItemCount;
    }
    }
    if (!loading && (totalItemCount - visibleItemCount)
    <= (firstVisibleItem + visibleThreshold)) {
    // End has been reached
    // Do something
    current_page++;

    onLoadMore(current_page);

    loading = true;
    }
    }

    //Start loading
    public abstract void onLoadMore(int current_page);
    }