感觉好久没有写博客了,又来搞事情。直接向上图,哈哈,这个效果是不是很酷呢(自认为挺酷的这个效果)。
其实这个非常的简单,就是覆写LinearLayoutManager,然后RecyclerView.setLayoutManager(manager)就OK了,是不是非常简单,上代码(Kotlin)
class EchelonLayoutManager(context:Context) : LinearLayoutManager(context){
private var mContext: Context?=null
private var mItemViewHeight:Int?=null
private var mItemViewWidget:Int?=null
private var mItemCount:Int?=null
private var mScrollOffset:Int= Int.MAX_VALUE
private var mScale:Float=0.9F
init {
mContext=context
//Item的宽度
mItemViewWidget=(getRvHorizontalSpace() * 0.87F).toInt()
//Item的高度
mItemViewHeight=(mItemViewWidget!! * 1.46F).toInt()
}
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
return RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT,RecyclerView.LayoutParams.WRAP_CONTENT)
}
override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
if(state?.itemCount == 0 || state!!.isPreLayout){
return
}
removeAndRecycleAllViews(recycler)
mItemViewWidget=(getRvHorizontalSpace()* 0.87F).toInt()
mItemViewHeight=(mItemViewWidget!! * 1.46F).toInt()
mItemCount=itemCount
mScrollOffset=Math.min(Math.max(mItemViewHeight!!,mScrollOffset),mItemCount!! * mItemViewHeight!!)
}
override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
var padingScrollOffset:Int=mScrollOffset + dy
mScrollOffset=Math.min(Math.max(mItemViewHeight!!,mScrollOffset+dy),mItemCount!! * mItemViewHeight!!)
layoutChild(recycler!!)
return mScrollOffset-padingScrollOffset+dy
}
override fun canScrollVertically(): Boolean {
return true
}
private fun layoutChild(recycler:RecyclerView.Recycler){
if (itemCount == 0) return
var bottomItemPosition:Int = Math.floor((mScrollOffset!! / mItemViewHeight!!).toDouble()).toInt()
var remainSpace:Int=getRvVerticalSpace() - mItemViewHeight!!
var bottomItemVisibleHeight:Int=mScrollOffset % mItemViewHeight!!
var offsetPercentRelativeToItemView:Float=bottomItemVisibleHeight * 1.0F / mItemViewHeight!!
var layoutInfos:ArrayList<ItemViewInfo