|
public class BannerAutoScrollRecyclerView extends RecyclerView { |
|
private static final int AUTO_SCROLL_PERIOD_MILLIS = 3500; |
|
private static final int SCROLL_WHAT = 1; |
|
private boolean mAutoScrollEnabled; |
|
private boolean mAutoScrolling; |
|
private ScrollHandler mHandler; |
|
|
|
private static class ScrollHandler extends Handler { |
|
private final WeakReference<BannerAutoScrollRecyclerView> mRecyclerViewWeakReference; |
|
|
|
ScrollHandler(BannerAutoScrollRecyclerView bannerAutoScrollRecyclerView) { |
|
this.mRecyclerViewWeakReference = new WeakReference(bannerAutoScrollRecyclerView); |
|
} |
|
|
|
public void handleMessage(Message message) { |
|
super.handleMessage(message); |
|
if (message.what == 1) { |
|
BannerAutoScrollRecyclerView bannerAutoScrollRecyclerView = (BannerAutoScrollRecyclerView) this.mRecyclerViewWeakReference.get(); |
|
if (bannerAutoScrollRecyclerView != null) { |
|
bannerAutoScrollRecyclerView.smoothScrollingToNextPosition(); |
|
} |
|
} |
|
} |
|
} |
|
|
|
public BannerAutoScrollRecyclerView(Context context) { |
|
super(context); |
|
} |
|
|
|
public BannerAutoScrollRecyclerView(Context context, AttributeSet attributeSet) { |
|
super(context, attributeSet); |
|
} |
|
|
|
public BannerAutoScrollRecyclerView(Context context, AttributeSet attributeSet, int i) { |
|
super(context, attributeSet, i); |
|
} |
|
|
|
public void startAutoScroll(boolean z) { |
|
if (!this.mAutoScrollEnabled) { |
|
this.mAutoScrollEnabled = true; |
|
this.mHandler = new ScrollHandler(this); |
|
startAutoScrolling(z); |
|
} |
|
} |
|
|
|
public void stopAutoScroll() { |
|
if (this.mAutoScrollEnabled) { |
|
this.mAutoScrollEnabled = false; |
|
this.mAutoScrolling = false; |
|
stopAutoScrolling(); |
|
this.mHandler = null; |
|
} |
|
} |
|
|
|
private void startAutoScrolling(boolean z) { |
|
sendScrollMessage(z); |
|
} |
|
|
|
private void stopAutoScrolling() { |
|
if (this.mHandler != null) { |
|
this.mHandler.removeMessages(1); |
|
} |
|
} |
|
|
|
public void onScrollStateChanged(int i) { |
|
super.onScrollStateChanged(i); |
|
switch (i) { |
|
case 0: |
|
if (this.mAutoScrollEnabled != 0 && this.mAutoScrolling == 0) { |
|
startAutoScrolling(false); |
|
} |
|
this.mAutoScrolling = false; |
|
break; |
|
case 1: |
|
if (this.mAutoScrollEnabled != 0 && this.mAutoScrolling == 0) { |
|
stopAutoScrolling(); |
|
return; |
|
} |
|
default: |
|
break; |
|
} |
|
} |
|
|
|
public void sendScrollMessage(boolean z) { |
|
if (this.mHandler != null) { |
|
this.mHandler.removeMessages(1); |
|
if (z) { |
|
this.mHandler.sendEmptyMessage(1); |
|
return; |
|
} |
|
this.mHandler.sendEmptyMessageDelayed(1, 3500); |
|
} |
|
} |
|
|
|
private void smoothScrollingToNextPosition() { |
|
if (getAdapter() == null || !(getAdapter() instanceof InfiniteBannerAdapter)) { |
|
throw new IllegalStateException("Adapter == null or Adapter class not InfiniteBannerAdapter"); |
|
} |
|
InfiniteBannerAdapter infiniteBannerAdapter = (InfiniteBannerAdapter) getAdapter(); |
|
LayoutManager layoutManager = getLayoutManager(); |
|
if (layoutManager instanceof LinearLayoutManager) { |
|
int findLastCompletelyVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition() + 1; |
|
if (findLastCompletelyVisibleItemPosition == infiniteBannerAdapter.getItemCount() - 1) { |
|
int sameBannerInCenterPosition = infiniteBannerAdapter.getSameBannerInCenterPosition(findLastCompletelyVisibleItemPosition); |
|
this.mAutoScrolling = true; |
|
scrollToPosition(sameBannerInCenterPosition); |
|
findLastCompletelyVisibleItemPosition = sameBannerInCenterPosition + 1; |
|
} |
|
this.mAutoScrolling = true; |
|
smoothScrollToPosition(findLastCompletelyVisibleItemPosition); |
|
sendScrollMessage(false); |
|
return; |
|
} |
|
throw new IllegalStateException("LinearLayoutManager has not been found"); |
|
} |
|
} |