@@ -30,6 +30,12 @@ public class DiscreteScrollView extends RecyclerView {
3030
3131 private List <ScrollStateChangeListener > scrollStateChangeListeners ;
3232 private List <OnItemChangedListener > onItemChangedListeners ;
33+ private Runnable notifyItemChangedRunnable = new Runnable () {
34+ @ Override
35+ public void run () {
36+ notifyCurrentItemChanged ();
37+ }
38+ };
3339
3440 private boolean isOverScrollEnabled ;
3541
@@ -97,6 +103,15 @@ public ViewHolder getViewHolder(int position) {
97103 return view != null ? getChildViewHolder (view ) : null ;
98104 }
99105
106+ @ Override
107+ public void scrollToPosition (int position ) {
108+ int currentPosition = layoutManager .getCurrentPosition ();
109+ super .scrollToPosition (position );
110+ if (currentPosition != position ) {
111+ notifyCurrentItemChanged ();
112+ }
113+ }
114+
100115 /**
101116 * @return adapter position of the current item or -1 if nothing is selected
102117 */
@@ -197,12 +212,17 @@ private void notifyCurrentItemChanged(ViewHolder holder, int current) {
197212 }
198213
199214 private void notifyCurrentItemChanged () {
215+ removeCallbacks (notifyItemChangedRunnable );
200216 if (onItemChangedListeners .isEmpty ()) {
201217 return ;
202218 }
203219 int current = layoutManager .getCurrentPosition ();
204220 ViewHolder currentHolder = getViewHolder (current );
205- notifyCurrentItemChanged (currentHolder , current );
221+ if (currentHolder == null ) {
222+ post (notifyItemChangedRunnable );
223+ } else {
224+ notifyCurrentItemChanged (currentHolder , current );
225+ }
206226 }
207227
208228 private class ScrollStateListener implements DiscreteScrollLayoutManager .ScrollStateListener {
@@ -216,6 +236,7 @@ public void onIsBoundReachedFlagChange(boolean isBoundReached) {
216236
217237 @ Override
218238 public void onScrollStart () {
239+ removeCallbacks (notifyItemChangedRunnable );
219240 if (scrollStateChangeListeners .isEmpty ()) {
220241 return ;
221242 }
@@ -256,12 +277,7 @@ public void onScroll(float currentViewPosition) {
256277
257278 @ Override
258279 public void onCurrentViewFirstLayout () {
259- post (new Runnable () {
260- @ Override
261- public void run () {
262- notifyCurrentItemChanged ();
263- }
264- });
280+ notifyCurrentItemChanged ();
265281 }
266282
267283 @ Override
0 commit comments