RecyclerView<第八篇>:实现无限循环滚动
2019-07-07 本文已影响6人
NoBugException
满足RecyclerView实现无限循环的需求还是有的,比如画廊。
使用RecyclerView横向展示网络图片可以当做一个画廊,如图:
16.gif以上图片已经是无限循环的了,那么无限循环该怎么实现呢?
[第一步]
Item数量控制到最大
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
[第二步]
获取数据时,计算角标和数据的对应关系(mData是Url数组)
String url = mData.get(position % mData.size());
就两步,很简单吧。
下面开始实现自动滚动,自动滚动有两个方法
recycleview.scrollToPosition(Integer.MAX_VALUE - 1);
或
recycleview.smoothScrollToPosition(Integer.MAX_VALUE - 1);
scrollToPosition是直接滚动到指定位置,没有滚动的动画效果,而smoothScrollToPosition却有滚动效果,效果图如下:
17.gif如图所示,添加smoothScrollToPosition之后,RecyclerView就会以默认的滚动速度滚动,当然这个速度是可控的,只需要重写LinearLayoutManager的smoothScrollToPosition方法即可,代码如下:
public class ScollLinearLayoutManager extends LinearLayoutManager {
private float MILLISECONDS_PER_INCH = 25f; //修改可以改变数据,越大速度越慢
private Context contxt;
public ScollLinearLayoutManager(Context context) {
super(context);
this.contxt = context;
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return ScollLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.density; //返回滑动一个pixel需要多少毫秒
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
//可以用来设置速度
public void setSpeedSlow(float x) {
MILLISECONDS_PER_INCH = contxt.getResources().getDisplayMetrics().density * 0.3f + (x);
}
}
效果如下:
18.gif最后,贴一下剩下的代码:
ScollLinearLayoutManager scollLinearLayoutManager = new ScollLinearLayoutManager(RecycleViewActivity.this);
scollLinearLayoutManager.setSpeedSlow(25);
scollLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recycleview.setLayoutManager(scollLinearLayoutManager);
recycleview.setHasFixedSize(false);
List<String> list = new ArrayList<>();
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883143&di=9e6ce0689e0ce16b08dbbb36cc4b3f64&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fphotoblog%2F1304%2F12%2Fc1%2F19787586_19787586_1365753960942.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883143&di=2c57b802fbc0e1c9251493cacbea5253&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fitbbs%2F1311%2F07%2Fc23%2F28416089_1383815777032_mthumb.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883143&di=d4ff8ebabc84fb3d90c3b73da9cf9dfd&imgtype=0&src=http%3A%2F%2Fpic18.nipic.com%2F20120206%2F2459179_100142574169_2.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883142&di=7dfeb9057802584220f1011bade40037&imgtype=0&src=http%3A%2F%2Fdpic.tiankong.com%2F2i%2Fjk%2FQJ8143828315.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883142&di=9666ee57dbca9d2f7b60da6a56e475a9&imgtype=0&src=http%3A%2F%2Fimage2.16888.com%2Fupload%2FImages%2F2013%2F11%2F2013111903381783187.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883142&di=b3454fed2daf6b220290f5e7dfb6b3fd&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fphotoblog%2F7%2F5%2F7%2F9%2F7579980%2F20095%2F22%2F1242966313996_mthumb.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883142&di=099881eacd536ddef398ae182c91fe6c&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fitbbs%2F1711%2F19%2Fc26%2F67203465_1511100627270.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883142&di=c8c479cb8dcb6dcc55bf02d3bece3be9&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fitbbs%2F1312%2F15%2Fc15%2F29649855_1387086454629_mthumb.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883141&di=c86c3b0fde47dccbec595b9205853700&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fphotoblog%2F1305%2F08%2Fc1%2F20663986_20663986_1367972059562.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883140&di=15470f8e31d4edfc5ae15bfd979a4956&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fitbbs%2F1310%2F21%2Fc9%2F27760991_1382320731679_mthumb.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883140&di=23911873eb850a43058ece96d9bd0b60&imgtype=0&src=http%3A%2F%2Fphoto.zlzsqc.com%2Farticle%2FUploadPic-whc%2F2009-8%2F2009820234321582.jpg");
list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1562508883140&di=fc97bb3521f3926350273ce8ac064b9f&imgtype=0&src=http%3A%2F%2Fpic5.nipic.com%2F20100120%2F1951702_101423039024_2.jpg");
adapter = new RecycleViewAdapter(RecycleViewActivity.this, list);
recycleview.setAdapter(adapter);
recycleview.smoothScrollToPosition(Integer.MAX_VALUE - 1);
public class RecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> mData;
private Context mContext;
private OnItemClickLitener mOnItemClickLitener;
public RecycleViewAdapter(Context context, List<String> mData) {
this.mContext = context;
this.mData = mData;
}
@Override
public int getItemViewType(int position) {
return position;
}
/**
* 设置数据
* @param mData
*/
public void setDatas(List<String> mData){
this.mData = mData;
}
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
String url = mData.get(position % mData.size());
if(!TextUtils.isEmpty(url)){
Glide.with(mContext).load(url).into(((ViewHolder)holder).imageView);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.recycleview_item, parent, false);
ViewHolder holder = new ViewHolder(itemView);
return holder;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.iv_image);
}
}
public interface OnItemClickLitener {
void onItemClick(View view, int position);
}
public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
this.mOnItemClickLitener = mOnItemClickLitener;
}
}
activity_recycle.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="200dp"/>
</RelativeLayout>
recycleview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp">
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
</LinearLayout>
[本章完...]