最近看了下VLayout,它主要用在超复杂界面布局,感觉确实比较好用,VirtualLayout是一个针对RecyclerView的LayoutManager扩展, 主要提供一整套布局方案和布局间的组件复用的问题。阿里开源出来的,先放官网地址:https://github.com/alibaba/vlayout
vlayout全称VirtualLayout,它是一个针对RecyclerView的LayoutManager扩展, 主要提供一整套布局方案和布局间的组件复用的问题。它通过定制化的LayoutManager,接管整个RecyclerView的布局逻辑;LayoutManager管理了一系列LayoutHelper,LayoutHelper负责具体布局逻辑实现的地方;每一个LayoutHelper负责页面某一个范围内的组件布局;不同的LayoutHelper可以做不同的布局逻辑,因此可以在一个RecyclerView页面里提供异构的布局结构,这就能比系统自带的LinearLayoutManager、GridLayoutManager等提供更加丰富的能力。同时支持扩展LayoutHelper来提供更多的布局能力。
版本请参考mvn repository上的最新版本(目前最新版本是1.2.36),最新的 aar 都会发布到 jcenter 和 MavenCentral 上,确保配置了这两个仓库源,然后引入aar依赖:
api("com.alibaba.android:vlayout:1.2.36@aar") {
transitive = true
}
Margin, padding就是外边距、内边距,概念与Android系统的margin, padding一样,但也有不同的地方:
对于LayoutHelper,调用
public void setPadding(int leftPadding, int topPadding, int rightPadding, int bottomPadding)
public void setMargin(int leftMargin, int topMargin, int rightMargin, int bottomMargin)
背景颜色或者背景图,这其实不是布局属性,但是由于在vlayout对视图进行了直接布局,不同区域的视图的父节点都是RecyclerView,如果想要针对某一块区域单独绘制背景,就很难做到了。vlayout框架对此做了特殊处理,对于非fix、非float类型的LayoutHelper,支持配置背景色或背景图。同样目前主要针对非fix类型的LayoutHelper实现这个特性。![](https://img-blog.csdnimg.cn/img_convert/db097eb3811e04f88fa0a9d1fb15f0a6.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=uf5c863dc&margin=[object Object]&originHeight=326&originWidth=595&originalType=url&ratio=1&status=done&style=none&taskId=u5e403e98-3670-4757-a7b3-c712f4d2973)
使用背景色
public void setBgColor(int bgColor)
使用背景图首先为LayoutManager提供一个ImageView简单工厂
this.mLayoutManager.setLayoutViewFactory(new LayoutViewFactory() {
@Override
public opinion generateLayoutView(@NonNull Context context) {
return new XXImageView(context);
}
});
再为LayoutHelper提设置图片加载的Listener
baseHelper.setLayoutViewBindListener(new BindListener(imgUrl));
baseHelper.setLayoutViewUnBindListener(new UnbindListener(imgUrl));
private static class BindListener implements BaseLayoutHelper.LayoutViewBindListener {
private String imgUrl;
public BindListener(String imgUrl) {
this.imgUrl = imgUrl;
}
@Override
public void onBind(View layoutView, BaseLayoutHelper baseLayoutHelper) {
//loading image
}
}
private static class UnbindListener implements BaseLayoutHelper.LayoutViewUnBindListener {
private String imgUrl;
public UnbindListener(String imgUrl) {
this. imgUrl = imgUrl;
}
@Override
public void onUnbind(View layoutView, BaseLayoutHelper baseLayoutHelper) {
//cancel loading image
}
}
为了保证布局过程中视图的高度一致,我们设计了aspectRatio属性,它是宽与高的比例,LayoutHelper里有aspectRatio属性,通过vlayout添加的视图的LayoutParams也有aspectRatio属性,后者的优先级比前者高,但含义不一样。
对于LayoutHelper,调用
public void setAspectRatio(float aspectRatio)
对于LayoutParams,调用
((VirutalLayoutManager.LayoutParams) layoutParams).mAspectRatio
LinearLayoutHelper的属性,LinearLayoutHelper是像ListView一样的线性布局,dividerHeight就是每个组件之间的间距。
![](https://img-blog.csdnimg.cn/img_convert/3f13dc0dfd603309bd1bb99732310568.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u26c67c93&margin=[object Object]&originHeight=321&originWidth=408&originalType=url&ratio=1&status=done&style=none&taskId=ubd8a4763-7fa5-4bb4-9eab-efe9a313048)
对于LinearLayoutHelper,调用
public void setDividerHeight(int dividerHeight)
ColumnLayoutHelper, GridLayoutHelper的属性,它们都是提供网格状的布局能力,建议使用GridLayoutHelper,它的能力更加强大,参考下文介绍。默认情况下,每个网格中每一列的宽度是一样的,通过weights属性,可以指定让每一列的宽度成比例分配,就像LinearLayout的weight属性一样。 weights属性是一个float数组,每一项代表某一列占父容器宽度的百分比,总和建议是100,否则布局会超出容器宽度;如果布局中有4列,那么weights的长度也应该是4;长度大于4,多出的部分不参与宽度计算;如果小于4,不足的部分默认平分剩余的空间。![](https://img-blog.csdnimg.cn/img_convert/390f850726681d5601cdc3ee441c33c7.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u8b59cd2e&margin=[object Object]&originHeight=320&originWidth=464&originalType=url&ratio=1&status=done&style=none&taskId=u96a5da84-7912-48b0-b097-7dfb83313e0)
对于ColumnLayoutHelper, GridLayoutHelper,调用
public void setWeights(float[] weights)
GridLayoutHelper与StaggeredGridLayoutHelper都有这两个属性,分别控制视图之间的垂直间距和水平间距。
![](https://img-blog.csdnimg.cn/img_convert/b76bd99da0b617bf9ebd7c332f2deb5a.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u02a09b11&margin=[object Object]&originHeight=320&originWidth=377&originalType=url&ratio=1&status=done&style=none&taskId=u71074afb-bd7d-49e6-a1c3-18a3d8647b2)
对于GridLayoutHelper, StaggeredGridLayoutHelper,调用
public void setHGap(int hGap)
public void setVGap(int vGap)
GridLayoutHelper的属性,参考于系统的GridLayoutManager,spanCount表示网格的列数,默认情况下每一个视图都占用一个网格区域,但通过提供自定义的spanSizeLookUp,可以指定某个位置的视图占用多个网格区域。![](https://img-blog.csdnimg.cn/img_convert/f384ab31aa0841b50497b7eff2bdd931.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u38b3a1cc&margin=[object Object]&originHeight=320&originWidth=568&originalType=url&ratio=1&status=done&style=none&taskId=u3e13ae14-9977-430d-87e4-b01461409f3)
使用spanCount调用
public void setSpanCount(int spanCount)
使用spanSizeLookup
public void setSpanSizeLookup(SpanSizeLookup spanSizeLookup)
GridLayoutHelper的属性,当一行里视图的个数少于spanCount值的时候,如果autoExpand为true,视图的总宽度会填满可用区域;否则会在屏幕上留空白区域。![](https://img-blog.csdnimg.cn/img_convert/4eb74d29c6f46e82ca8b4ab3b4256db6.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u25f686ae&margin=[object Object]&originHeight=320&originWidth=698&originalType=url&ratio=1&status=done&style=none&taskId=u4ec3ecc0-f6c8-47f7-b0eb-461338fe4f5)
接口:
public void setAutoExpand(boolean isAutoExpand)
StaggeredGridLayoutHelper中有这个属性,与GridLayoutHelper里的spanCount类似,控制瀑布流的列数。
接口:
public void setLane(int lane)
fix类型的LayoutHelper,在可能需要设置一个相对父容器四个边的偏移量,比如整个页面里有一个固定的标题栏添加在vlayout容器上,vlayout内部的fix类型视图不希望与外部的标题有所重叠,那么就可以设置一个fixAreaAdjuster来做偏移。![](https://img-blog.csdnimg.cn/img_convert/26379f9fc89af738095769a24b312fe5.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u11339c2d&margin=[object Object]&originHeight=320&originWidth=593&originalType=url&ratio=1&status=done&style=none&taskId=uc694ef69-4702-4037-b4e9-094bca2c7e5)
接口:
public void setAdjuster(FixAreaAdjuster adjuster)
FixLayoutHelper, ScrollFixLayoutHelper, FloatLayoutHelper的属性,表示吸边时的基准位置,有四个取值,分别是TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT。x和y是相对这四个位置的偏移量,最终的偏移量还要受上述的fixAreaAdjuster影响。
![](https://img-blog.csdnimg.cn/img_convert/1f1830dc02079bdf6ad75b596dc0f30d.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u389b82b3&margin=[object Object]&originHeight=406&originWidth=424&originalType=url&ratio=1&status=done&style=none&taskId=ue8348637-3485-421f-ad1c-679f9d32ed8)
设置基准调用
public void setAlignType(int alignType)
设置偏移量调用
public void setX(int x)
public void setY(int y)
ScrollFixLayoutHelper的属性,取值有SHOW_ALWAYS, SHOW_ON_ENTER, SHOW_ON_LEAVE。
接口:
public void setShowType(int showType)
StickyLayoutHelper的属性,当视图的位置在屏幕范围内时,视图会随页面滚动而滚动;当视图的位置滑出屏幕时,StickyLayoutHelper会将视图固定在顶部(stickyStart = true)或者底部(stickyStart = false),固定的位置支持设置偏移量offset。![](https://img-blog.csdnimg.cn/img_convert/2a0b2d50db096c9aaa34ea2d2b2b0c9b.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=ua26026b9&margin=[object Object]&originHeight=368&originWidth=780&originalType=url&ratio=1&status=done&style=none&taskId=ucea10595-fccd-490b-8113-9f5ccfbd670)
接口:
public void setStickyStart(boolean stickyStart)
public void setOffset(int offset)
上面我们已经详细介绍的各种LayoutHelper以及它的各种属性,现在,我们通过demo来进行实例演示。
我们activity只要进行一些简单的配置就可以了:
VirtualLayoutManager manager = new VirtualLayoutManager(this);
recycler.setLayoutManager(manager);
DelegateAdapter adapter = new DelegateAdapter(manager, true);
adapter.addAdapter(new DelegateRecyclerAdapter(this,new LinearLayoutHelper()));
recycler.setAdapter(adapter);
对于adapter ,我们继承DelegateAdapter来实现,代码很简单,如下:
public LayoutHelper onCreateLayoutHelper() {
return helper;
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewholder(inflater.inflate(R.layout.item, parent, false));
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((MyViewholder) holder).text.setText(position + 1 + "");
}
public int getItemCount() {
return 60;
}
public class MyViewholder extends RecyclerView.ViewHolder {
private TextView text;
public MyViewholder(View view) {
super(view);
text = (TextView) view.findViewById(R.id.text);
}
}
效果图:
![](https://img-blog.csdnimg.cn/img_convert/e395e3009ab86a4b0f0afa153b1bdf79.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u0a236023&margin=[object Object]&originHeight=539&originWidth=317&originalType=url&ratio=1&status=done&style=none&taskId=uae4a814c-a5ad-4493-bdb8-27adeb14cd2)
我们只要把LinearLayouthelper改成Gridlayouthelper就可以了:
VirtualLayoutManager manager = new VirtualLayoutManager(this);
recycler.setLayoutManager(manager);
DelegateAdapter adapter = new DelegateAdapter(manager, true);
adapter.addAdapter(new DelegateRecyclerAdapter(this,new GridLayoutHelper(3)));
recycler.setAdapter(adapter);
效果图:
![](https://img-blog.csdnimg.cn/img_convert/eb5f6dac6de87ea33d04ea29d25d7065.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=ub2da2342&margin=[object Object]&originHeight=552&originWidth=316&originalType=url&ratio=1&status=done&style=none&taskId=u13ec4dca-c3d9-4b4d-bfb9-7ffcf14eaf9)
还是直接修改LayoutHelper就可以了:
VirtualLayoutManager manager = new VirtualLayoutManager(this);
recycler.setLayoutManager(manager);
DelegateAdapter adapter = new DelegateAdapter(manager, true);
//StaggeredGridLayoutHelper(int num,int gap)
//num为每行显示数目,gap为两个item的边距
adapter.addAdapter(new StaggeredAdapter(this,new StaggeredGridLayoutHelper(3,20)));
recycler.setAdapter(adapter);
为了做成瀑布流的效果,我们对每个item进行一个随机高度的设置:
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ViewGroup.LayoutParams layoutParams = ((MyViewholder) holder).text.getLayoutParams();
layoutParams.height = 260 + position % 7 * 20;
((MyViewholder) holder).text.setLayoutParams(layoutParams);
((MyViewholder) holder).text.setText(position + 1 + "");
}
效果图:
![](https://img-blog.csdnimg.cn/img_convert/ee7384ac14ca1235898a7e6ec9bd0a4d.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=ue2ebc47e&margin=[object Object]&originHeight=530&originWidth=317&originalType=url&ratio=1&status=done&style=none&taskId=uf6c10043-3dc5-4472-9833-eb8ffb9b49e)
对于fixlayout类型的,我们需要先后添加一次LinearLayoutHelper和FixLayoutHelper。
VirtualLayoutManager manager = new VirtualLayoutManager(this);
recycler.setLayoutManager(manager);
DelegateAdapter adapter = new DelegateAdapter(manager, true);
adapter.addAdapter(new DelegateRecyclerAdapter(this, new LinearLayoutHelper()));
adapter.addAdapter(new ScrollFixAdapter(this, new FixLayoutHelper(FixLayoutHelper.BOTTOM_LEFT, 200, 200), recycler));
recycler.setAdapter(adapter);
效果图:
![](https://img-blog.csdnimg.cn/img_convert/2ed1cdfe04b5346425b181c38ef60680.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=ue8fa8e39&margin=[object Object]&originHeight=541&originWidth=316&originalType=url&ratio=1&status=done&style=none&taskId=u4fb328d5-c155-44b3-b5c2-01d5b090933)
同上,代码是差不多的,不过官方所说的标签,置顶等功能。并不能实现。官方demo也并没有实现此功能。虽然我们可以通过点击图片来进行置顶。但是具体功能感觉和fixlayout无异。文末有demo。博友们自己下载试试就知道了。
代码如下:
VirtualLayoutManager manager = new VirtualLayoutManager(this);
recycler.setLayoutManager(manager);
DelegateAdapter adapter = new DelegateAdapter(manager, true);
adapter.addAdapter(new DelegateRecyclerAdapter(this,new ColumnLayoutHelper()));
recycler.setAdapter(adapter);
效果图:
![](https://img-blog.csdnimg.cn/img_convert/7068bbf901c84446eb1af71f4ca2096c.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u202952ff&margin=[object Object]&originHeight=552&originWidth=316&originalType=url&ratio=1&status=done&style=none&taskId=u2d7f3915-851a-46bd-9cd8-80eb6d703a3)
VirtualLayoutManager manager = new VirtualLayoutManager(this);
recycler.setLayoutManager(manager);
DelegateAdapter adapter = new DelegateAdapter(manager, true);
adapter.addAdapter(new DelegateRecyclerAdapter(this,new SingleLayoutHelper()));
recycler.setAdapter(adapter);
效果图:
![](https://img-blog.csdnimg.cn/img_convert/abb8e8185354ce4c6da04a207428d344.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u8d3ae6ca&margin=[object Object]&originHeight=554&originWidth=321&originalType=url&ratio=1&status=done&style=none&taskId=u7667c807-f7da-49ff-84e3-5d5b0a9b8b4)
一拖N布局,听起来感觉高大上,不过我并不知道这玩意能用在什么地方…
VirtualLayoutManager manager = new VirtualLayoutManager(this);
recycler.setLayoutManager(manager);
DelegateAdapter adapter = new DelegateAdapter(manager, true);
OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper(3);
adapter.addAdapter(new OnePlusNRecyclerAdapter(this,helper));
recycler.setAdapter(adapter);
效果图:
![](https://img-blog.csdnimg.cn/img_convert/58d860958a1a3bc77e17994162a88d49.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=ud1201098&margin=[object Object]&originHeight=552&originWidth=317&originalType=url&ratio=1&status=done&style=none&taskId=uc979d16d-8fb9-4905-82e2-3e373ad3b96)
VirtualLayoutManager manager = new VirtualLayoutManager(this);
recycler.setLayoutManager(manager);
DelegateAdapter adapter = new DelegateAdapter(manager, true);
adapter.addAdapter(new FloatAdapter(this,new FloatLayoutHelper()));
adapter.addAdapter(new DelegateRecyclerAdapter(this,new LinearLayoutHelper()));
recycler.setAdapter(adapter);
效果图:
![](https://img-blog.csdnimg.cn/img_convert/47ad421d24bff2fbd9a1bca55bec6b36.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u853d7e57&margin=[object Object]&originHeight=540&originWidth=320&originalType=url&ratio=1&status=done&style=none&taskId=ua1d682b8-d73c-4f45-b7b7-dbfda1cf02a)
这个吸顶和吸底效果还是比较强大,自我感觉可以深入研究的就这个和FloatLayoutHelper了。具体代码如下:
VirtualLayoutManager manager = new VirtualLayoutManager(this);
recycler.setLayoutManager(manager);
DelegateAdapter adapter = new DelegateAdapter(manager, true);
//在顶部时需先添加sticklayout,在底部时最后添加sticklayout
StickyLayoutHelper helper = new StickyLayoutHelper(true);
// adapter.addAdapter(new StickRecyclerAdapter(this, helper, recycler));
// adapter.addAdapter(new DelegateRecyclerAdapter(this, new LinearLayoutHelper()));
//顶部和实体合二为一
adapter.addAdapter(new DelegateRecyclerAdapter(this, helper));
adapter.addAdapter(new DelegateRecyclerAdapter(this, new LinearLayoutHelper()));
//底部
// StickyLayoutHelper helper = new StickyLayoutHelper(false);
// adapter.addAdapter(new DelegateRecyclerAdapter(this, new LinearLayoutHelper()));
// adapter.addAdapter(new StickRecyclerAdapter(this, helper));
recycler.setAdapter(adapter);
效果图:顶部:
![](https://img-blog.csdnimg.cn/img_convert/7ad1cb0d3b1cb6b00f0691f50d058b44.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u54554d19&margin=[object Object]&originHeight=547&originWidth=316&originalType=url&ratio=1&status=done&style=none&taskId=u12c4b78b-c618-4ea2-a635-21fedce7c7a)
实体和顶部合二为一:
![](https://img-blog.csdnimg.cn/img_convert/9cdf3dfde66f14683a6b86bc8f4269bb.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=uc2f18818&margin=[object Object]&originHeight=539&originWidth=320&originalType=url&ratio=1&status=done&style=none&taskId=ufe1ffef2-bf7a-47e5-a56d-e6fa2a0e862)