github地址(完整Demo)
https://github.com/zhouxu88/CustomLayoutManager
最近瀏覽github,發(fā)現(xiàn)了一個(gè)特別炫酷的效果,可以用來讓你的APP逼格更高,特此和大家分享一下。
效果圖

自定義LayoutManager.gif
自定義customLayoutManager會(huì)幫你處理一部分放置、回收、以及滾動(dòng)的邏輯,你只需關(guān)注你想隨著滾動(dòng)改變的屬性,他是如何改變的,以及達(dá)到哪個(gè)值時(shí)會(huì)被回收即可。所以我們只需要新建一個(gè)自己的layoutManager并繼承CustomLayoutManager,在這里先提一下,CustomLayoutManager有幾個(gè)默認(rèn)的屬性是你可以直接使用的。
protected Context context;
//子view的寬度
protected int mDecoratedChildWidth;
//子view的高度
protected int mDecoratedChildHeight;
//子view距離屏幕最左的偏移,也可以理解為第一個(gè)子view在初始狀態(tài)下距離屏幕左側(cè)的位移,默認(rèn)居中
protected int startLeft;
//子view距離屏幕頂部的位移,默認(rèn)居中
protected int startTop;
//主要隨滑動(dòng)所改變的屬性的偏移量,考慮到view的屬性有int,有float所以這邊統(tǒng)一用float表示偏移
protected float offset;
//相鄰兩個(gè)子view間,主要隨滑動(dòng)而改變的屬性的差值(比如隨滑動(dòng)改變的是view的角度,那么這個(gè)值就是各個(gè)view之間的角度間隔)
protected float interval;
繼承CustomLayoutManager之后你必須實(shí)現(xiàn)這幾個(gè)方法
public class MyLayoutManager extends CustomLayoutManager{
//默認(rèn)isClockWise為true
public MyLayoutManager(Context context) {
super(context);
}
//isClockWise為true時(shí)從左往右排列,不然則從右往左排列
public MyLayoutManager(Context context, boolean isClockWise) {
super(context, isClockWise);
}
//這個(gè)方法會(huì)設(shè)置默認(rèn)的interval變量,之后可以直接使用interval
@Override
protected float setInterval() {
return 0;
}
//初始化方法,你可以在這里初始化自己的一些參數(shù),比如實(shí)現(xiàn)圓弧布局的半徑,或是更改一些默認(rèn)的屬性,比如startLeft,startTop
@Override
protected void setUp() {
}
//itemView就是每一個(gè)子view,targetOffset就是其對(duì)應(yīng)的將要改變到的屬性值,你可以在這里根據(jù)targetOffset對(duì)子view的一些屬性進(jìn)行設(shè)置
@Override
protected void setItemViewProperty(View itemView, float targetOffset) {
}
}
此外還有6個(gè)你可以選擇重寫的方法
//當(dāng)子view的屬性超過這個(gè)值時(shí),就會(huì)被回收掉
@Override
protected float maxRemoveOffset() {
return getHorizontalSpace() - startLeft;
}
//當(dāng)子view的屬性小于這個(gè)值時(shí),就會(huì)被回收掉
@Override
protected float minRemoveOffset() {
return -mDecoratedChildWidth-getPaddingLeft() - startLeft;
}
//當(dāng)view的屬性等于targetOffset時(shí),此view基于初始位置的x坐標(biāo),一般返回targetOffset
@Override
protected int calItemLeftPosition(float targetOffset) {
return targetOffset;
}
//當(dāng)view的屬性等于targetOffset時(shí),此view基于初始位置的y坐標(biāo),一般返回0
@Override
protected int calItemTopPosition(float targetOffset) {
return 0;
}
//這邊返回在滾動(dòng)時(shí)子view主要改變的屬性的值
@Override
protected float propertyChangeWhenScroll(View itemView) {
return itemView.getLeft()-startLeft;
}
//滑動(dòng)產(chǎn)生的偏移dx與offset的比例,默認(rèn)為1
protected float getDistanceRatio(){
return 1f;
}