Android使用RecycleView实现跑马灯和轮播图效果
龙旋
共 4362字,需浏览 9分钟
·
2021-06-11 17:48
先看下效果图:
利用Recycleview实现这个效果最主要的一个方法就是这个方法
mSnapHelper = new LinearSnapHelper();
mSnapHelper.attachToRecyclerView(mRecyclerView);
这个方法的作用是使得RecycelView像ViewPager一样的效果,一次只能滑一页,而且居中显示。
然后利用Handler来实现无限轮播的效果,在Adapter中要注意
public int getItemCount() {
return Integer.MAX_VALUE;
}
Glide.with(mContext).load(mDatas.get(position % mDatas.size())).into(mImageView);
getItemCount中返回一个无穷大达到无限录播的效果,在填充数据时取余防止索引越界,然后自定义Layoutmanger
public class MyBannerManger extends LinearLayoutManager {
private RecyclerView mRecyclerView;
private final InterValHander mHander;
private static final int INTERVAL = 3000;
private boolean mIsLayoutComplete = false;
private static int position = 1;
private LinearSnapHelper mSnapHelper;
public MyBannerManger(Context context, RecyclerView recyclerView) {
super(context);
setOrientation(HORIZONTAL);
mRecyclerView = recyclerView;
mHander = new InterValHander(this);
mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
mHander.removeCallbacksAndMessages(null);
}else if(event.getAction() == MotionEvent.ACTION_UP){
mHander.sendEmptyMessageDelayed(position,INTERVAL);
}
return false;
}
});
}
public void onAttachedToWindow(RecyclerView view) {
super.onAttachedToWindow(view);
mSnapHelper = new LinearSnapHelper();
mSnapHelper.attachToRecyclerView(mRecyclerView);
}
public void onLayoutCompleted(RecyclerView.State state) {
super.onLayoutCompleted(state);
if (!mIsLayoutComplete) {
mHander.sendEmptyMessageDelayed(position,INTERVAL);
}
mIsLayoutComplete = true;
}
private RecyclerView getRecyclerView(){
return mRecyclerView;
}
static class InterValHander extends Handler {
private WeakReference<MyBannerManger> mWeakReference;
private MyBannerManger mBannerManger;
public InterValHander(MyBannerManger myBannerManger) {
mWeakReference = new WeakReference<>(myBannerManger);
mBannerManger = myBannerManger;
}
public void handleMessage(Message msg) {
LogUtils.LogE(msg.toString());
mBannerManger.getRecyclerView().smoothScrollToPosition(position);
sendEmptyMessageDelayed(0,INTERVAL);
position++;
}
}
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LogUtils.LogE("smoothScrollToPosition");
LinearSmoothScroller smoothScroller =
new LinearSmoothScroller(recyclerView.getContext()) {
// 返回:滑过1px时经历的时间(ms)。
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
float f = 150f / displayMetrics.densityDpi;
LogUtils.LogE("F = "+f);
return 0.5f;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
public void onScrollStateChanged(int state) {
if(state == SCROLL_STATE_IDLE){
position = getPosition(mSnapHelper.findSnapView(this))+1;
}
}
处理逻辑:
public class RActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private String[] mStrings = {"南京南站","龙眠大道","学则路","仙鹤门","仙鹤名苑","紫东国际创意园!"};
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_r);
mRecyclerView = findViewById(R.id.rv);
List<String> list = Arrays.asList(Images.imageUrls);
mRecyclerView.setLayoutManager(new MyBannerManger(this,mRecyclerView));
RAdapter rAdapter = new RAdapter(this, list);
mRecyclerView.setAdapter(rAdapter);
}
}
到这里就结束啦。
评论