xmuSistone / VegaLayoutManager

√A customized LayoutManager - fade and shrink the head itemView when scrolling.
1.85k stars 232 forks source link

送给有需要的人 #36

Open salmanit opened 6 years ago

salmanit commented 6 years ago

本来想看源码学习的,看着好多代码,累了,就想着自己弄, 看了看效果,感觉就是线性的,唯一的区别就是第一个item有个变化而已。 代码如下,继承LinearLayoutManager重写如下的方法即可,基本可以达到效果,好像有人想要横向的,逻辑也差不多。

` var oldChild0:View?=null; override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State?): Int { val result=super.scrollVerticallyBy(dy, recycler, state) if(childCount>1){ val child1=getChildAt(1) val child0=getChildAt(0) if(oldChild0!=null&&oldChild0!=child0){ resetChild(oldChild0!!) } oldChild0=child0 var scale=0.7f+0.3f*(child1.top)/getDecoratedMeasuredHeight(child1)

        child0.pivotX=child0.width/2f
        child0.pivotY=child0.height/1f
        child0.scaleX=scale
        child0.scaleY=scale
        child0.translationY=getDecoratedMeasuredHeight(child1)-child1.top.toFloat()
        child0.alpha=scale
        if(scale<0.7){
            resetChild(child0)
            removeAndRecycleView(child0,recycler)
        }
    }
    return  result
}
private fun resetChild(child:View){
    child.apply {
        pivotY=this.width/2f
        pivotX=this.height/1f
        scaleX=1f;
        scaleY=1f;
        alpha=1f;
        translationY=0f
    }
}

`

thundeGG commented 6 years ago

大佬,有demo吗

salmanit commented 6 years ago

@7hundeR 上边代码也差不多了,你继承LinearLayoutManager 就完事了。也没啥demo吧,recyclerView该咋用咋用,就是layoutManager用这个就行了。 `import android.content.Context import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.view.View

class LayoutManagerScaleFirst:LinearLayoutManager{ constructor(context: Context?) : super(context) constructor(context: Context?, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout)

private var oldChild0:View?=null;
private val scaleMinFactor=0.7f //from 0 to 1
override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State?): Int {
    val result=super.scrollVerticallyBy(dy, recycler, state)
    if(childCount>1){
        val child1=getChildAt(1)
        val child0=getChildAt(0)
        if(oldChild0!=null&&oldChild0!=child0){
            resetChild(oldChild0!!)
        }
        oldChild0=child0
        val scale=scaleMinFactor+(1-scaleMinFactor)*(child1.top)/getDecoratedMeasuredHeight(child1)
        println("scrollVerticallyBy====${scale}==childCount=${childCount}===${getChildAt(0).height}====${getChildAt(1).top}")
        viewAnimate(child0,scale,getDecoratedMeasuredHeight(child1)-child1.top.toFloat())
        if(scale<scaleMinFactor){
            resetChild(child0)
            removeAndRecycleView(child0,recycler)
        }
    }
    return  result
}
private fun resetChild(child:View){
    viewAnimate(child,1f,0f)
}

private fun viewAnimate(child: View,factor:Float,transY:Float){
    child.apply {
        pivotX=this.width/2f
        pivotY=this.height/1f
        scaleX=factor;
        scaleY=factor;
        alpha=factor;
        translationY=transY
    }
}

}`

thundeGG commented 5 years ago

感谢大佬

thundeGG commented 5 years ago

加上margin或者padding后能看到顶上缩小的view

salmanit commented 5 years ago

不知道你说的啥样子,上下margin用decoration就行,左右的margin应该没啥影响吧,我这里是滑动的效果图,没看出有啥问题。 或者你具体说下,给谁添加margin?另外我就是个菜鸟。 https://www.jianshu.com/p/42f9997e3ce0