变速翻页滚动
public class PzAutoPageScrollRecyclerView extends RecyclerView implements Runnable {
private String TAG = "PzAutoPageScrollRecyclerView" ;
private int itemPageCount;
private int itemAllCount;
private int scrollTime;
private int nowPage = 0 ;
private HandlerThread mHandlerThread = new HandlerThread ( "PzAutoPageScrollRecyclerView" ) ;
private Handler mHandler;
public PzAutoPageScrollRecyclerView ( @NonNull Context context) {
super ( context) ;
}
public PzAutoPageScrollRecyclerView ( @NonNull Context context, @Nullable AttributeSet attrs) {
super ( context, attrs) ;
}
public PzAutoPageScrollRecyclerView ( @NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super ( context, attrs, defStyleAttr) ;
}
public void init ( int itemPageCount, int itemAllCount, int scrollTime) {
this . itemPageCount = itemPageCount;
this . itemAllCount = itemAllCount;
this . scrollTime = scrollTime;
mHandlerThread. start ( ) ;
mHandler = new Handler ( mHandlerThread. getLooper ( ) ) ;
}
public void starAuto ( int scrollTime) {
this . scrollTime = scrollTime;
mHandler. postDelayed ( this , scrollTime) ;
}
void stopAuto ( ) {
removeCallbacks ( this ) ;
}
void last ( ) {
if ( ( nowPage - 1 ) >= 0 ) {
nowPage-- ;
smoothScrollToPosition ( nowPage * itemPageCount) ;
}
}
void next ( ) {
if ( ( nowPage + 1 ) * itemPageCount < itemAllCount) {
nowPage++ ;
smoothScrollToPosition ( nowPage * itemPageCount) ;
}
}
@Override
public void run ( ) {
if ( itemPageCount < itemAllCount) {
if ( ( nowPage + 1 ) * itemPageCount < itemAllCount) {
nowPage++ ;
smoothScrollToPosition ( nowPage * itemPageCount) ;
} else {
nowPage = 1 ;
smoothScrollToPosition ( nowPage * itemPageCount) ;
nowPage = 0 ;
smoothScrollToPosition ( nowPage * itemPageCount) ;
}
} else {
mHandler. postDelayed ( this , scrollTime) ;
}
}
@Override
public void onScrollStateChanged ( int state) {
super . onScrollStateChanged ( state) ;
if ( getScrollState ( ) == 0 ) {
mHandler. postDelayed ( this , scrollTime) ;
}
}
public int getItemAllCount ( ) {
return itemAllCount;
}
public void setItemAllCount ( int itemAllCount) {
this . itemAllCount = itemAllCount;
}
public int getItemPageCount ( ) {
return itemPageCount;
}
public void setItemPageCount ( int itemPageCount) {
this . itemPageCount = itemPageCount;
}
public int getScrollTime ( ) {
return scrollTime;
}
public void setScrollTime ( int scrollTime) {
this . scrollTime = scrollTime;
}
}
匀速滑动滚动
public class MyRecyclerView extends RecyclerView {
Handler mHandler = new Handler ( ) ;
public MyRecyclerView ( @NonNull Context context) {
super ( context) ;
}
public MyRecyclerView ( @NonNull Context context, @Nullable AttributeSet attrs) {
super ( context, attrs) ;
}
public MyRecyclerView ( @NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super ( context, attrs, defStyleAttr) ;
}
void starAuto ( final int h, final int time) {
mHandler. post ( new Runnable ( ) {
@Override
public void run ( ) {
scrollBy ( 0 , h) ;
mHandler. postDelayed ( this , time) ;
}
} ) ;
}
void stopAuto ( ) {
mHandler. removeCallbacksAndMessages ( null) ;
}
}
补充