android 全屏轮播,Android 使用RecyclerView实现轮播图

一、需求

之前一篇博客使用ViewPager实现轮播图《Android ViewPager实现循环轮播图》,但是ViewPager有个天生的缺陷是View无法重用,此外ViewPager的滑动过程会频繁requestLayout,尽管可以通过addViewInLayout和removeViewInLayout配合PagerAdapter 的startUpdate和finishUpdate可以减少重绘,但在ListView和RecyclerView中仍然达不到最好的效果。因此,使用一种新的方式十分必要。

ee5d2d5346cc24b0203f5cb1c5dbd11a.gif

f51f72c4afaabfecae28f1a4ed615745.gif

二、代码实现

RecyclerPagerView

public class RecyclerPagerView extends RecyclerView implements Handler.Callback {

private static final long TASK_TIMEOUT = 3000;

public OnPageChangeListener onPageChangeListener;

private final Handler mRecyclerHandler;

private final int MSG_PLAY_NEXT = 112233;

private volatile boolean isPlaying = false;

private boolean lastIsPlayState = false;

private int realPosition = -1;

public RecyclerPagerView(Context context) {

this(context,null);

}

public RecyclerPagerView(Context context, @Nullable AttributeSet attrs) {

this(context, attrs,0);

}

public RecyclerPagerView(Context context, @Nullable AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

mRecyclerHandler = new Handler(Looper.getMainLooper(),this);

}

public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener) {

this.onPageChangeListener = onPageChangeListener;

if(this.onPageChangeListener!=null){

addOnScrollListener(this.onPageChangeListener);

int currentItem = getCurrentItem();

this.onPageChangeListener.onPageSelection(currentItem);

}

}

public int getCurrentItem(){

LinearLayoutManager linearLayoutManager = (LinearLayoutManager) getLayoutManager();

return linearLayoutManager.findFirstVisibleItemPosition();

}

public void setCurrentItem(int position,boolean isAnimate){

Adapter adapter = getAdapter();

if(adapter==null || adapter.getItemCount()<=position){

return;

}

if(!isAnimate)

{

scrollToPosition(position);

}else {

smoothScrollToPosition(position);

}

}

public void setCurrentItem(int position ){

setCurrentItem(position,true);

}

@Override

public boolean onTouchEvent(MotionEvent e) {

int action = e.getAction();

if(action==MotionEv

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值