RecyclerView梯度效果的滑动

感觉好久没有写博客了,又来搞事情。直接向上图,哈哈,这个效果是不是很酷呢(自认为挺酷的这个效果)。

其实这个非常的简单,就是覆写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> = ArrayList()
   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值