Skip to content

Commit

Permalink
Fixed onItemChanged not being called after scrollToPosition re #147
Browse files Browse the repository at this point in the history
  • Loading branch information
yarolegovich committed Aug 1, 2020
1 parent 9d97982 commit a7c7372
Showing 1 changed file with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public class DiscreteScrollView extends RecyclerView {

private List<ScrollStateChangeListener> scrollStateChangeListeners;
private List<OnItemChangedListener> onItemChangedListeners;
private Runnable notifyItemChangedRunnable = new Runnable() {
@Override
public void run() {
notifyCurrentItemChanged();
}
};

private boolean isOverScrollEnabled;

Expand Down Expand Up @@ -97,6 +103,15 @@ public ViewHolder getViewHolder(int position) {
return view != null ? getChildViewHolder(view) : null;
}

@Override
public void scrollToPosition(int position) {
int currentPosition = layoutManager.getCurrentPosition();
super.scrollToPosition(position);
if (currentPosition != position) {
notifyCurrentItemChanged();
}
}

/**
* @return adapter position of the current item or -1 if nothing is selected
*/
Expand Down Expand Up @@ -197,12 +212,17 @@ private void notifyCurrentItemChanged(ViewHolder holder, int current) {
}

private void notifyCurrentItemChanged() {
removeCallbacks(notifyItemChangedRunnable);
if (onItemChangedListeners.isEmpty()) {
return;
}
int current = layoutManager.getCurrentPosition();
ViewHolder currentHolder = getViewHolder(current);
notifyCurrentItemChanged(currentHolder, current);
if (currentHolder == null) {
post(notifyItemChangedRunnable);
} else {
notifyCurrentItemChanged(currentHolder, current);
}
}

private class ScrollStateListener implements DiscreteScrollLayoutManager.ScrollStateListener {
Expand All @@ -216,6 +236,7 @@ public void onIsBoundReachedFlagChange(boolean isBoundReached) {

@Override
public void onScrollStart() {
removeCallbacks(notifyItemChangedRunnable);
if (scrollStateChangeListeners.isEmpty()) {
return;
}
Expand Down Expand Up @@ -256,12 +277,7 @@ public void onScroll(float currentViewPosition) {

@Override
public void onCurrentViewFirstLayout() {
post(new Runnable() {
@Override
public void run() {
notifyCurrentItemChanged();
}
});
notifyCurrentItemChanged();
}

@Override
Expand Down

1 comment on commit a7c7372

@dontlo
Copy link

@dontlo dontlo commented on a7c7372 Jan 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After I call the scrollToPosition method, I received the adapterPosition param in OnItemChangedListener. But when I using infinitedAdapter.getRealPosition method to get the real position, the result always would be added one. It's confusing me for several days, I am wondering if it is a bug or just I using it incorrectly.

Please sign in to comment.