Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preloader support for StaggeredGridLayputManager #5324

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 +48,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 Down
Loading