RecyclerView 实现背景 或者 item 循环滚动,无限滚动
先看效果:
之后有时间了,再上传一下,自定义imageview 实现大图长图循环滚动的view;
自动滚动实现方法:首先自定义LinearLayoutManager,重写smoothScrollToPosition方法,然后实现个自己的LinearSmoothScroller ,在calculateSpeedPerPixel返回个自定义的速率值。看过源码就会很容易理解,这里就不上源码了,可以自行搜索一下,循环滚动的话adapter里 getItemCount() 返回集合size,如果是一个方法无限滚动下去,就返回 Integer.MAX_VALUE;代码里
recyclerView.smoothScrollToPosition(Integer.MAX_VALUE / 2);就不需要监听滚动了。
代码如下:
public class CustomLinerLayoutManager extends LinearLayoutManager {
private float speedPerPixel = 0.001f;//滚动速率 越小越快 越大越慢
private Context context;
public CustomLinerLayoutManager(Context context) {
super(context);
this.context = context;
}
public CustomLinerLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustomLinerLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setSpeedPerPixel(float speedPerPixel) {
this.speedPerPixel = speedPerPixel;
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private class CenterSmoothScroller extends LinearSmoothScroller {
CenterSmoothScroller(Context context) {
super(context);
}
@Nullable
@Override //滑动到目标索引
public PointF computeScrollVectorForPosition(int targetPosition) {
return CustomLinerLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
@Override // 滑动位置
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {// 控制滑动速度的
return speedPerPixel;
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;
}
}
public void setSpeedSlow() {
speedPerPixel = context.getResources().getDisplayMetrics().density * 3f;
}
public void setSpeedFast() {
speedPerPixel = context.getResources().getDisplayMetrics().density * 0.03f;
}
}
RecyclerView 添加 OnScrollListener 监听,滑动过程中,监听当前的索引位置 或者 在顶部或者是在底部,方法很多,可以参考实现 或者自行是实现,在底部了就让它往顶部滚动,在顶部了就让它往底部滚动,速率大小可以通过CustomLinerLayoutManager 自定义 ,写个方法外部set 进去也可以。
RecyclerView.OnScrollListener onScrollListenerTwo = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
isVisBottom(recyclerView);
}
};
public void isVisBottom(RecyclerView recyclerView) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
if (!recyclerView.canScrollHorizontally(-1)) {//当前在顶部
recyclerView.smoothScrollToPosition(linearLayoutManager.getItemCount() - 1);
}
if (!recyclerView.canScrollHorizontally(1)) {//当前在底部
recyclerView.smoothScrollToPosition(0);
}
}