RecyclerView实现抖音纵向滚动ViewPager效果
使用RecyclerView实现抖音纵向滚动ViewPager效果,供大家参考,具体内容如下
重写LinearLayoutManager,在onAttachedToWindow方法中使用PagerSnapHelper设置RecyclerView条目加载方式为每次滚动加载一页
classMyLinearLayoutManager:LinearLayoutManager{
privatelateinitvarmPagerSnapHelper:PagerSnapHelper
privatevarmOnViewPagerListener:OnViewPagerListener?=null
privatelateinitvarmRecyclerView:RecyclerView
privatevarmDrift:Int=0//位移,用来判断移动方向
constructor(context:Context):this(context,OrientationHelper.VERTICAL)
constructor(context:Context,orientation:Int):this(context,orientation,false)
constructor(context:Context,orientation:Int,reverseLayout:Boolean):super(context,orientation,reverseLayout){
mPagerSnapHelper=PagerSnapHelper()
}
overridefunonAttachedToWindow(view:RecyclerView){
super.onAttachedToWindow(view)
mPagerSnapHelper.attachToRecyclerView(view)//设置RecyclerView每次滚动一页
mRecyclerView=view
mRecyclerView.addOnChildAttachStateChangeListener(mChildAttachStateChangeListener)
}
/**
*滑动状态的改变
*缓慢拖拽->SCROLL_STATE_DRAGGING
*快速滚动->SCROLL_STATE_SETTLING
*空闲状态->SCROLL_STATE_IDLE
*@paramstate
*/
overridefunonScrollStateChanged(state:Int){
if(state==RecyclerView.SCROLL_STATE_IDLE){
valviewIdle=mPagerSnapHelper.findSnapView(this)
valpositionIdle=getPosition(viewIdle!!)
if(mOnViewPagerListener!=null&&childCount==1){
mOnViewPagerListener!!.onPageSelected(positionIdle,positionIdle==itemCount-1)
}
}
}
/**
*布局完成后调用
*@paramstate
*/
overridefunonLayoutCompleted(state:RecyclerView.State?){
super.onLayoutCompleted(state)
if(mOnViewPagerListener!=null)mOnViewPagerListener!!.onLayoutComplete()
}
/**
*监听竖直方向的相对偏移量
*/
overridefunscrollVerticallyBy(dy:Int,recycler:RecyclerView.Recycler?,state:RecyclerView.State?):Int{
this.mDrift=dy
returnsuper.scrollVerticallyBy(dy,recycler,state)
}
/**
*监听水平方向的相对偏移量
*/
overridefunscrollHorizontallyBy(dx:Int,recycler:RecyclerView.Recycler?,state:RecyclerView.State?):Int{
this.mDrift=dx
returnsuper.scrollHorizontallyBy(dx,recycler,state)
}
/**
*设置监听
*@paramlistener
*/
funsetOnViewPagerListener(listener:OnViewPagerListener){
this.mOnViewPagerListener=listener
}
privatevalmChildAttachStateChangeListener=object:RecyclerView.OnChildAttachStateChangeListener{
overridefunonChildViewAttachedToWindow(view:View){
}
overridefunonChildViewDetachedFromWindow(view:View){
if(mDrift>=0){
if(mOnViewPagerListener!=null)mOnViewPagerListener!!.onPageRelease(true,getPosition(view))
}else{
if(mOnViewPagerListener!=null)mOnViewPagerListener!!.onPageRelease(false,getPosition(view))
}
}
}
interfaceOnViewPagerListener{
/*释放的监听*/
funonPageRelease(isNext:Boolean,position:Int)
/*选中的监听以及判断是否滑动到底部*/
funonPageSelected(position:Int,isBottom:Boolean)
/*布局完成的监听*/
funonLayoutComplete()
}
}
重写RecyclerView条目内容主布局满屏填充
classMyImageView:ImageView{
constructor(context:Context):this(context,null!!)
constructor(context:Context,attr:AttributeSet):this(context,attr,0)
constructor(context:Context,attr:AttributeSet,defStyleAttr:Int):super(context,attr,defStyleAttr)
overridefunonMeasure(widthMeasureSpec:Int,heightMeasureSpec:Int){
valwidth=View.getDefaultSize(0,widthMeasureSpec)
valheight=View.getDefaultSize(0,heightMeasureSpec)
setMeasuredDimension(width,height)
}
}
代码参考:LayoutManagerGroup
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。