当前位置: 首页 > 工具软件 > vlayout > 使用案例 >

VLayout详解

锺离辰沛
2023-12-01

概述

最近看了下VLayout,它主要用在超复杂界面布局,感觉确实比较好用,VirtualLayout是一个针对RecyclerView的LayoutManager扩展, 主要提供一整套布局方案和布局间的组件复用的问题。阿里开源出来的,先放官网地址:https://github.com/alibaba/vlayout

VLayout简介

vlayout全称VirtualLayout,它是一个针对RecyclerView的LayoutManager扩展, 主要提供一整套布局方案和布局间的组件复用的问题。它通过定制化的LayoutManager,接管整个RecyclerView的布局逻辑;LayoutManager管理了一系列LayoutHelper,LayoutHelper负责具体布局逻辑实现的地方;每一个LayoutHelper负责页面某一个范围内的组件布局;不同的LayoutHelper可以做不同的布局逻辑,因此可以在一个RecyclerView页面里提供异构的布局结构,这就能比系统自带的LinearLayoutManager、GridLayoutManager等提供更加丰富的能力。同时支持扩展LayoutHelper来提供更多的布局能力。

主要功能

  • 默认通用布局实现,解耦所有的View和布局之间的关系: Linear, Grid, 吸顶, 浮动, 固定位置等1:LinearLayoutHelper: 线性布局2:GridLayoutHelper: Grid布局, 支持横向的colspan3:FixLayoutHelper: 固定布局,始终在屏幕固定位置显示4:ScrollFixLayoutHelper: 固定布局,但之后当页面滑动到该图片区域才显示, 可以用来做返回顶部或其他书签等5:FloatLayoutHelper: 浮动布局,可以固定显示在屏幕上,但用户可以拖拽其位置6:ColumnLayoutHelper: 栏格布局,都布局在一排,可以配置不同列之间的宽度比值7:SingleLayoutHelper: 通栏布局,只会显示一个组件View8:OnePlusNLayoutHelper: 一拖N布局,可以配置1-5个子元素9:StickyLayoutHelper: stikcy布局, 可以配置吸顶或者吸底10:StaggeredGridLayoutHelper: 瀑布流布局,可配置间隔高度/宽度
  • 上述默认实现里可以大致分为两类:一是非fix类型布局,像线性、Grid、栏格等,它们的特点是布局在整个页面流里,随页面滚动而滚动;另一类就是fix类型的布局,它们的子节点往往不随页面滚动而滚动。
  • 所有除布局外的组件复用,VirtualLayout将用来管理大的模块布局组合,扩展了RecyclerView,使得同一RecyclerView内的组件可以复用,减少View的创建和销毁过程。

使用方法

版本请参考mvn repository上的最新版本(目前最新版本是1.2.36),最新的 aar 都会发布到 jcenter 和 MavenCentral 上,确保配置了这两个仓库源,然后引入aar依赖:

api("com.alibaba.android:vlayout:1.2.36@aar") {
        transitive = true
    }

demo示例

LayoutHelper功能介绍

margin, padding

Margin, padding就是外边距、内边距,概念与Android系统的margin, padding一样,但也有不同的地方:

  • 它不是整个RecyclerView页面的margin和padding,它是每一块LayoutHelper所负责的区域的margin和padding。
  • 一个页面里可以有多个LayoutHelper,意味着不同LayoutHelper可以设置不同的margin和padding。
  • LayoutHelper的margin和padding与页面RecyclerView的margin和padding可以共存。
  • 目前主要针对非fix类型的LayoutHelper实现了margin和padding,fix类型LayoutHelper内部没有相对位置关系,不处理边距。![](https://img-blog.csdnimg.cn/img_convert/6771301c409169f07b72542907e70081.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=ufd8a2b69&margin=[object Object]&originHeight=320&originWidth=589&originalType=url&ratio=1&status=done&style=none&taskId=u7efc599b-11ce-43da-9b6c-be8d352ea22)

对于LayoutHelper,调用

public void setPadding(int leftPadding, int topPadding, int rightPadding, int bottomPadding)
public void setMargin(int leftMargin, int topMargin, int rightMargin, int bottomMargin)

bgColor, bgImg

背景颜色或者背景图,这其实不是布局属性,但是由于在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

为了保证布局过程中视图的高度一致,我们设计了aspectRatio属性,它是宽与高的比例,LayoutHelper里有aspectRatio属性,通过vlayout添加的视图的LayoutParams也有aspectRatio属性,后者的优先级比前者高,但含义不一样。

  • LayoutHelper定义的aspectRatio,指的是一行视图整体的宽度与高度之比,当然整体的宽度是减去了RecyclerView和对应的LayoutHelper的margin, padding。
  • 视图的LayoutParams定义的aspectRatio,指的是在LayoutHelper计算出视图宽度之后,用来确定视图高度时使用的,它会覆盖通过LayoutHelper的aspectRatio计算出来的视图高度,因此具备更高优先级。![](https://img-blog.csdnimg.cn/img_convert/27bc9c25e4bd9680b9fe46668d26dbc4.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=u8178d967&margin=[object Object]&originHeight=326&originWidth=589&originalType=url&ratio=1&status=done&style=none&taskId=u194dfcc1-5f64-4afc-b228-b6565b4c43a)

对于LayoutHelper,调用

public void setAspectRatio(float aspectRatio)

对于LayoutParams,调用

((VirutalLayoutManager.LayoutParams) layoutParams).mAspectRatio

dividerHeight

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)

weights

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)

vGap, hGap

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)

spanCount, spanSizeLookup

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)

autoExpand

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)

lane

StaggeredGridLayoutHelper中有这个属性,与GridLayoutHelper里的spanCount类似,控制瀑布流的列数。
接口:

public void setLane(int lane)

fixAreaAdjuster

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)

alignType, x, y

FixLayoutHelper, ScrollFixLayoutHelper, FloatLayoutHelper的属性,表示吸边时的基准位置,有四个取值,分别是TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT。x和y是相对这四个位置的偏移量,最终的偏移量还要受上述的fixAreaAdjuster影响。

  • TOP_LEFT:基准位置是左上角,x是视图左边相对父容器的左边距偏移量,y是视图顶边相对父容器的上边距偏移量;
  • TOP_RIGHT:基准位置是右上角,x是视图右边相对父容器的右边距偏移量,y是视图顶边相对父容器的上边距偏移量;
  • BOTTOM_LEFT:基准位置是左下角,x是视图左边相对父容器的左边距偏移量,y是视图底边相对父容器的下边距偏移量;
  • BOTTOM_RIGHT:基准位置是右下角,x是视图右边相对父容器的右边距偏移量,y是视图底边相对父容器的下边距偏移量;

![](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)

showType

ScrollFixLayoutHelper的属性,取值有SHOW_ALWAYS, SHOW_ON_ENTER, SHOW_ON_LEAVE。

  • SHOW_ALWAYS:与FixLayoutHelper的行为一致,固定在某个位置;
  • SHOW_ON_ENTER:默认不显示视图,当页面滚动到这个视图的位置的时候,才显示;
  • SHOW_ON_LEAVE:默认不显示视图,当页面滚出这个视图的位置的时候显示;![](https://img-blog.csdnimg.cn/img_convert/6ba7e153c8c4089c14b595cc1565d33e.webp?x-oss-process=image/format,png#clientId=u63b860cd-dc5e-4&from=paste&id=uc42aedcc&margin=[object Object]&originHeight=416&originWidth=780&originalType=url&ratio=1&status=done&style=none&taskId=ue8351d26-fc86-4004-a59d-621b2701b2f)

接口:

public void setShowType(int showType)

stickyStart, offset

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来进行实例演示。

LinearLayoutHelper

我们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)

GridLayoutHelper

我们只要把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)

StaggeredGridLayoutHelper

还是直接修改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)

FixLayoutHelper

对于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)

ScrollFixLayoutHelper

同上,代码是差不多的,不过官方所说的标签,置顶等功能。并不能实现。官方demo也并没有实现此功能。虽然我们可以通过点击图片来进行置顶。但是具体功能感觉和fixlayout无异。文末有demo。博友们自己下载试试就知道了。

ColumnLayoutHelper

代码如下:

        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)

SingleLayoutHelper

        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)

OnePlusNLayoutHelper

一拖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)

FloatLayoutHelper

        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)

StickyLayoutHelper

这个吸顶和吸底效果还是比较强大,自我感觉可以深入研究的就这个和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)

 类似资料: