淘寶開(kāi)源庫(kù)VLayout實(shí)踐

最近淘寶出了vlayout,剛開(kāi)始看淘寶的文檔的時(shí)候還是有點(diǎn)懵芜果,后來(lái)自己也總結(jié)規(guī)劃了一下蔽氨,寫(xiě)了一個(gè)比較好看的demo,順便在這里總結(jié)一下跨算。

VLayout是什么爆土,說(shuō)白了就是用一個(gè)原生RecycelerView加上VLayout來(lái)實(shí)現(xiàn)在一個(gè)頁(yè)面上比較復(fù)雜的布局并且有一個(gè)比較好的復(fù)用,在RecyclerView里同時(shí)有GridLayout布局诸蚕,瀑布流布局步势,浮動(dòng)布局等VLayout提供的九大布局,這也是淘寶客戶(hù)端首頁(yè)加載不同布局的方法背犯。

好了坏瘩,簡(jiǎn)單介紹到這里,首先我們先導(dǎo)入VLayout:

compile ('com.alibaba.android:vlayout:版本@aar') {
    transitive = true
}

具體的版本請(qǐng)看github里VLayout給出的版本號(hào)∧海現(xiàn)在最新是1.0.6
接著我們就可以開(kāi)始引用VLayout了,VLayout的通用代碼如下:

VirtualLayoutManager manager = new VirtualLayoutManager(this);
recyclerview.setLayoutManager(manager);
DelegateAdapter adapter =new DelegateAdapter(manager, true);
  • 其中VirtualLayoutManager它繼承自LinearLayoutManager倔矾;引入了 LayoutHelper 的概念,它負(fù)責(zé)具體的布局邏輯;VirtualLayoutManager管理了一系列LayoutHelper哪自,將具體的布局能力交給LayoutHelper來(lái)完成丰包。
  • DelegateAdapter是VLayout專(zhuān)門(mén)為L(zhǎng)ayoutHelper定制的Adapter,我們把裝載有各種布局的LayoutHelper的Adapter放進(jìn)DelegateAdapter里最后在RecyclerView.setAdapter(DelegateAdapter);就可以加載出復(fù)雜的布局提陶。
  • 或許你們會(huì)問(wèn)什么是LayoutHelper烫沙,這個(gè)問(wèn)題問(wèn)得好,就是VLayout提供的九種默認(rèn)通用布局隙笆,解耦所有的View和布局之間的關(guān)系: Linear, Grid, 吸頂, 浮動(dòng), 固定位置等具體,名稱(chēng)和功能如下:
  1. LinearLayoutHelper: 線性布局
  2. GridLayoutHelper: Grid布局锌蓄, 支持橫向的colspan
  3. StaggeredGridLayoutHelper: 瀑布流布局,可配置間隔高度/寬度
  4. FixLayoutHelper: 固定布局撑柔,始終在屏幕固定位置顯示
  5. ScrollFixLayoutHelper: 固定布局瘸爽,但之后當(dāng)頁(yè)面滑動(dòng)到該圖片區(qū)域才顯示, 可以用來(lái)做返回頂部或其他書(shū)簽等
  6. FloatLayoutHelper: 浮動(dòng)布局,可以固定顯示在屏幕上铅忿,但用戶(hù)可以拖拽其位置
  7. ColumnLayoutHelper: 欄格布局剪决,都布局在一排,可以配置不同列之間的寬度比值
  8. SingleLayoutHelper: 通欄布局檀训,只會(huì)顯示一個(gè)組件View
  9. OnePlusNLayoutHelper: 一拖N布局柑潦,可以配置1-5個(gè)子元素
  10. StickyLayoutHelper: stikcy布局, 可以配置吸頂或者吸底

這就是九種布局對(duì)應(yīng)的類(lèi)峻凫,我們可以用著九個(gè)類(lèi)實(shí)現(xiàn)各種復(fù)雜的布局渗鬼,下面我會(huì)一一介紹每個(gè)布局和效果,以便更直觀的看到效果:

  • LinearLayoutHelper: 線性布局荧琼,就是實(shí)現(xiàn)ListView的效果很簡(jiǎn)單譬胎,直接看代碼:
public class LinearLayoutHelperActivity extends Activity{
    public static RecyclerView recyclerview;
    public static DelegateRecyclerAdapter delegateRecyclerAdapter;
    public DelegateAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        recyclerview=(RecyclerView)findViewById(R.id.recyclerview);
        VirtualLayoutManager manager = new VirtualLayoutManager(this);
        recyclerview.setLayoutManager(manager);
        adapter =new DelegateAdapter(manager, true);

        adapter.addAdapter(init(this));
        recyclerview.setAdapter(adapter);
    }

    public static DelegateRecyclerAdapter init(Context context){
        LinearLayoutHelper linearLayoutHelper=new LinearLayoutHelper();
        //設(shè)置間隔高度
        linearLayoutHelper.setDividerHeight(5);
        //設(shè)置布局底部與下個(gè)布局的間隔
        linearLayoutHelper.setMarginBottom(20);
        //設(shè)置間距
        linearLayoutHelper.setMargin(20,20,20,20);
        delegateRecyclerAdapter=new DelegateRecyclerAdapter(context,linearLayoutHelper,"LinearLayoutHelper");
        return delegateRecyclerAdapter;
    }
}

而DelegateRecyclerAdapter的代碼如下:

public class DelegateRecyclerAdapter extends DelegateAdapter.Adapter{
    public Context context;
    private LayoutHelper helper;
    private LayoutInflater inflater;
    private String name;

    public DelegateRecyclerAdapter(Context context,LayoutHelper helper,String name){
        this.inflater = LayoutInflater.from(context);
        this.helper = helper;
        this.context=context;
        this.name=name;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return this.helper;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(inflater.inflate(R.layout.layout_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if(position%2==0){
                holder.itemView.setBackgroundColor(0xaa3F51B5);
            }else{
                holder.itemView.setBackgroundColor(0xccFF4081);
            }
        MyViewHolder myViewHolder=(MyViewHolder)holder;
        myViewHolder.name.setText(name+position+"");
    }

    @Override
    public int getItemCount() {
        return 9;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView name;
        public MyViewHolder(View itemView) {
            super(itemView);
            name=(TextView)itemView.findViewById(R.id.item_name);
        }
    }

}

這里需要說(shuō)的就是在Adapter類(lèi)里我們需要繼承Vlayout的DelegateAdapter.Adapter類(lèi),然后多回調(diào)onCreateLayoutHelper()方法命锄,返回我們傳進(jìn)去的LayoutHelper類(lèi)堰乔,其他的和普通的RecyclerView是一個(gè)樣的。DelegateRecyclerAdapter在我們后面也有多次引用脐恩。其他要說(shuō)的就是我們初始完LinearLayoutHelper后生產(chǎn)DelegateAdapter.Adapter類(lèi)再賦給DelegateAdapter镐侯,然后然后RecyclerView在setAdapter()就這樣。


image.png
  • GridLayoutHelper: Grid布局驶冒, 支持橫向的colspan析孽,也很簡(jiǎn)單,代碼如下:
public static DelegateRecyclerAdapter init(Context context){
        GridLayoutHelper gridLayoutHelper=new GridLayoutHelper(4);
        //自定義設(shè)置某些位置的Item的占格數(shù)
        gridLayoutHelper.setSpanSizeLookup(new GridLayoutHelper.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position >5) {
                    return 2;
                }else {
                    return 1;
                }
            }
        });
        //是否填滿可用區(qū)域
        gridLayoutHelper.setAutoExpand(false);
        delegateRecyclerAdapter=new DelegateRecyclerAdapter(context,gridLayoutHelper,"GridLayoutHelper");
        return delegateRecyclerAdapter;
    }

其他代碼很上面的一樣只怎。


img2.PNG
  • StaggeredGridLayoutHelper: 瀑布流布局,可配置間隔高度/寬度,代碼如下:
 public static StaggeredAdapter init(Context context){
    StaggeredGridLayoutHelper staggeredGridLayoutHelper=new StaggeredGridLayoutHelper(3,20);
    staggeredAdapter=new StaggeredAdapter(context,staggeredGridLayoutHelper,"StaggeredGridLayoutHelper");
    return staggeredAdapter;
 }

在StaggeredAdapter里我們?cè)趏nBindViewHolder里用

ViewGroup.LayoutParams layoutParams = ((MyViewholder) holder).text.getLayoutParams();
layoutParams.height = 260 + position % 7 * 20;
((MyViewholder) holder).text.setLayoutParams(layoutParams);

來(lái)實(shí)現(xiàn)高度不一致怜俐,效果如下:


img3.PNG
  • FixLayoutHelper: 固定布局身堡,始終在屏幕固定位置顯示,代碼如下:
public static FixLayoutAdapter initFixLayoutHelper(Context context){
    FixLayoutHelper fixLayoutHelper=new FixLayoutHelper(FixLayoutHelper.BOTTOM_LEFT, 200, 200);
    FixLayoutAdapter fixLayoutAdapter=new FixLayoutAdapter(context,fixLayoutHelper,"fixlayouthelp");
    return fixLayoutAdapter;
}

除了有FixLayoutHelper.BOTTOM_LEFT之外,還有FixLayoutHelper.TOP_LEFT拍鲤,F(xiàn)ixLayoutHelper.BOTTOM_RIGHT贴谎,F(xiàn)ixLayoutHelper.TOP_RIGHT, 200,200分別對(duì)應(yīng)偏移量x,y汞扎,效果如下:


img4.PNG

fixlayouthelp區(qū)域塊就是FixLayoutHelper了。

  • ScrollFixLayoutHelper: 固定布局擅这,但之后當(dāng)頁(yè)面滑動(dòng)到該圖片區(qū)域才顯示, 可以用來(lái)做返回頂部或其他書(shū)簽等澈魄,代碼如下:
public static FixLayoutAdapter initScrollFixLayout(Context context){
    ScrollFixLayoutHelper scrollFixLayoutHelper = new ScrollFixLayoutHelper(15,15);
    //show_always:總是顯示
    //show_on_enter:當(dāng)頁(yè)面滾動(dòng)到這個(gè)視圖的位置的時(shí)候,才顯示
    //show_on_leave:當(dāng)頁(yè)面滾出這個(gè)視圖的位置的時(shí)候顯示
    scrollFixLayoutHelper.setShowType(ScrollFixLayoutHelper.SHOW_ON_ENTER);
    return new FixLayoutAdapter(context, scrollFixLayoutHelper,"scrollfixlayouthelper");
}

代碼很簡(jiǎn)單仲翎,看效果:


img5.PNG

ScrollFixLayoutHelper繼承自FixLayoutHelper痹扇,不同的是showType來(lái)決定這個(gè)布局的Item是否顯示,可以用來(lái)做一些返回頂部之類(lèi)的按鈕溯香,

  1. SHOW_ALWAYS:與FixLayoutHelper的行為一致鲫构,固定在某個(gè)位置;
  2. SHOW_ON_ENTER:默認(rèn)不顯示視圖玫坛,當(dāng)頁(yè)面滾動(dòng)到這個(gè)視圖的位置的時(shí)候结笨,才顯示;
  3. SHOW_ON_LEAVE:默認(rèn)不顯示視圖湿镀,當(dāng)頁(yè)面滾出這個(gè)視圖的位置的時(shí)候顯示炕吸;

這里效果不明顯,等集合所有布局之后大家就可以看很直觀的效果

  • FloatLayoutHelper: 浮動(dòng)布局勉痴,可以固定顯示在屏幕上赫模,但用戶(hù)可以拖拽其位置,代碼如下:
public static FixLayoutAdapter initFloatLayoutHelper(Context context){
    FloatLayoutHelper floatLayoutHelper=new FloatLayoutHelper();
    floatLayoutHelper.setDefaultLocation(20,250);
    FixLayoutAdapter fixLayoutAdapter=new FixLayoutAdapter(context,floatLayoutHelper,"floatlayouthelper");
    return  fixLayoutAdapter;
}

效果如下:

img6.gif

其中setDefaultLocation()使用來(lái)設(shè)置他的初始位置的蚀腿,setAlignType(表示吸邊時(shí)的基準(zhǔn)位置嘴瓤,默認(rèn)左上角,有四個(gè)取值莉钙,分別是TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT)

  • ColumnLayoutHelper: 欄格布局廓脆,都布局在一排,可以配置不同列之間的寬度比值磁玉,代碼如下:
 public static ColumnLayoutAdapter initColumnLayout(Context context){
    ColumnLayoutHelper columnLayoutHelper=new ColumnLayoutHelper();
    columnLayoutHelper.setWeights(new float[]{20,20,20,20,20});
    columnLayoutHelper.setMarginBottom(20);
    ColumnLayoutAdapter columnLayoutAdapter=new ColumnLayoutAdapter(context,columnLayoutHelper,"ColumnLayoutHelper");
    return columnLayoutAdapter;
}

ColumnLayoutHelper需要設(shè)置Weights停忿,是一個(gè)float數(shù)組,總和為100蚊伞,否則超出布局席赂。效果圖如下:


img7.PNG
  • SingleLayoutHelper: 通欄布局,只會(huì)顯示一個(gè)組件View时迫,這里建議設(shè)置Adapter個(gè)數(shù)為1颅停,因?yàn)樗椭粫?huì)顯示一欄,假如有多個(gè)可能會(huì)出現(xiàn)一些問(wèn)題掠拳,本人實(shí)測(cè)個(gè)數(shù)多時(shí)會(huì)出點(diǎn)問(wèn)題癞揉。代碼如下:
public static SingleLayoutAdapter initSingleLayout(Context context){
    SingleLayoutHelper singleLayoutHelper=new SingleLayoutHelper();
    //設(shè)置間距
    singleLayoutHelper.setMargin(20,20,20,20);
    SingleLayoutAdapter singleLayoutAdapter=new SingleLayoutAdapter(context,singleLayoutHelper,"SingleLayoutHelper");
    return singleLayoutAdapter;
}

效果圖如下:

img8.PNG
  • OnePlusNLayoutHelper: 一拖N布局,可以配置1-5個(gè)子元素,根據(jù)個(gè)數(shù)的不同所呈現(xiàn)的界面也是不一樣的,不同個(gè)數(shù)效果如下:

//個(gè)數(shù)為1

個(gè)數(shù)為1.PNG

//個(gè)數(shù)為2

個(gè)數(shù)為2.PNG

//個(gè)數(shù)為3

個(gè)數(shù)為3.PNG

//個(gè)數(shù)為4

個(gè)數(shù)為4.PNG

//個(gè)數(shù)為5

個(gè)數(shù)為5.PNG

代碼如下:

 public static OnePlusNLayoutAdapter initOnePlusNLayout(Context context){
    OnePlusNLayoutHelper onePlusNLayoutHelper=new OnePlusNLayoutHelper();
    //設(shè)置布局底部與下個(gè)布局的間隔
    onePlusNLayoutHelper.setMarginBottom(20);
    OnePlusNLayoutAdapter onePlusNLayoutAdapter=new OnePlusNLayoutAdapter(context,onePlusNLayoutHelper,"OnePlusNLayoutHelper");
    return onePlusNLayoutAdapter;
}

  • StickyLayoutHelper: stikcy布局, 可以配置吸頂或者吸底喊熟,代碼如下:
public static StickyLayoutAdapter initStickyLayoutHelper(Context context){
    StickyLayoutHelper stickyLayoutHelper=new StickyLayoutHelper();
    return new StickyLayoutAdapter(context,stickyLayoutHelper);
}

效果圖如下:

img10.gif
  • 最后假如只是單單加載其中的一個(gè)布局其實(shí)意義不大柏肪,VLayout只最大的意義在于加載多個(gè)布局并且保持一個(gè)很好的復(fù)用,所以我們把上面的所有布局一起加載起來(lái)芥牌,代碼如下:
public class AllActivity extends Activity{

    private RecyclerView recyclerview;
    private DelegateAdapter delegateAdapter ;
    final List<DelegateAdapter.Adapter> adapters = new LinkedList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        recyclerview=(RecyclerView)findViewById(R.id.recyclerview);

        initView();
    }

    public void initView(){
        RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        recyclerview.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0,10);

        adapters.add(LinearLayoutHelperActivity.init(this));
        adapters.add(ColumnLayoutHelperActivity.initColumnLayout(this));
        adapters.add(GridLayoutHelperActivity.init(this));
        adapters.add(FixLayoutHelperActivity.initFixLayoutHelper(this));
        adapters.add(ScrollFixLayoutHelperActivity.initScrollFixLayout(this));
        adapters.add(SingleLayoutHelperActivity.initSingleLayout(this));
        adapters.add(OnePlusNLayoutHelperActivity.initOnePlusNLayout(this));
        adapters.add(FloatLayoutHelperActivity.initFloatLayoutHelper(this));
        adapters.add(StickyLayoutHelperActivity.initStickyLayoutHelper(this));
        adapters.add(StaggeredGridLayoutHelperActivity.init(this));

        VirtualLayoutManager manager = new VirtualLayoutManager(this);
        recyclerview.setLayoutManager(manager);
        delegateAdapter = new DelegateAdapter(manager);

        delegateAdapter.setAdapters(adapters);
        recyclerview.setAdapter(delegateAdapter);
    }

}

要注意的是DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager, hasConsistItemType);里當(dāng)hasConsistItemType=true的時(shí)候烦味,不論是不是屬于同一個(gè)子adapter,相同類(lèi)型的item都能復(fù)用壁拉。表示它們共享一個(gè)類(lèi)型谬俄。 當(dāng)hasConsistItemType=false的時(shí)候皆的,不同子adapter之間的類(lèi)型不共享箕昭。

效果如下圖:

img11.gif

最后源碼demo,代碼都在這https://github.com/jack921/ProjectVLayout

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市拂封,隨后出現(xiàn)的幾起案子案铺,更是在濱河造成了極大的恐慌蔬芥,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件控汉,死亡現(xiàn)場(chǎng)離奇詭異笔诵,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)姑子,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)乎婿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人街佑,你說(shuō)我怎么就攤上這事谢翎。” “怎么了沐旨?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵森逮,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我磁携,道長(zhǎng)褒侧,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任谊迄,我火速辦了婚禮闷供,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘统诺。我一直安慰自己歪脏,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布粮呢。 她就那樣靜靜地躺著唾糯,像睡著了一般怠硼。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上移怯,一...
    開(kāi)封第一講書(shū)人閱讀 51,274評(píng)論 1 300
  • 那天,我揣著相機(jī)與錄音这难,去河邊找鬼舟误。 笑死,一個(gè)胖子當(dāng)著我的面吹牛姻乓,可吹牛的內(nèi)容都是我干的嵌溢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼蹋岩,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼赖草!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起剪个,我...
    開(kāi)封第一講書(shū)人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤秧骑,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后扣囊,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體乎折,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年侵歇,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了骂澄。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡惕虑,死狀恐怖坟冲,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情溃蔫,我是刑警寧澤健提,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站酒唉,受9級(jí)特大地震影響矩桂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜痪伦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一侄榴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧网沾,春花似錦癞蚕、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)攒射。三九已至,卻和暖如春恒水,著一層夾襖步出監(jiān)牢的瞬間会放,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工钉凌, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留咧最,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓御雕,卻偏偏與公主長(zhǎng)得像矢沿,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子酸纲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容