Skip to content

Instantly share code, notes, and snippets.

@brownsoo
Created August 22, 2017 09:43
Show Gist options
  • Save brownsoo/376f9fdaa7cb763982b6274cfe837736 to your computer and use it in GitHub Desktop.
Save brownsoo/376f9fdaa7cb763982b6274cfe837736 to your computer and use it in GitHub Desktop.
Updates visible item views in RecyclerView
private void updateVisibleTaskIndex() {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
int first = linearLayoutManager.findFirstVisibleItemPosition();
int last = linearLayoutManager.findLastVisibleItemPosition();
RecyclerView.ViewHolder viewHolder;
for (int i = first; i <= last; i++) {
viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
if (viewHolder instanceof TaskViewHolder) { //TaskViewHolder is custom holder
// Update view components in ViewHolder
((TaskViewHolder) viewHolder).updateIndex(i - taskSection.getIndex());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment