Skip to content

Commit

Permalink
Preloader support for StaggeredGridLayputManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Yermolenko authored and sjudd committed Mar 20, 2024
1 parent bafbf41 commit 95df923
Showing 1 changed file with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.bumptech.glide.integration.recyclerview;
package com.leia.holopix.util.glide;

import android.widget.AbsListView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
* Converts {@link androidx.recyclerview.widget.RecyclerView.OnScrollListener} events to {@link
Expand Down Expand Up @@ -47,11 +49,33 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

int firstVisible = layoutManager.findFirstVisibleItemPosition();
int visibleCount = Math.abs(firstVisible - layoutManager.findLastVisibleItemPosition());
int itemCount = recyclerView.getAdapter().getItemCount();
int firstVisible = RecyclerView.NO_POSITION;
int visibleCount = 0;

RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;

firstVisible = linearLayoutManager.findFirstVisibleItemPosition();
visibleCount = Math.abs(firstVisible - linearLayoutManager.findLastVisibleItemPosition());
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;

int[] firstVisiblePositions = staggeredGridLayoutManager.findFirstVisibleItemPositions(null);

if (firstVisiblePositions.length > 0) {
firstVisible = firstVisiblePositions[0];

int[] lastVisiblePositions = staggeredGridLayoutManager.findLastVisibleItemPositions(null);

if (lastVisiblePositions.length > 0) {
int lastVisible = lastVisiblePositions[0];
visibleCount = Math.abs(firstVisible - lastVisible);
}
}
}

if (firstVisible != lastFirstVisible
|| visibleCount != lastVisibleCount
Expand All @@ -62,4 +86,4 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
lastItemCount = itemCount;
}
}
}
}

0 comments on commit 95df923

Please sign in to comment.