Vlayout的全面介绍

发布时间:2023-05-20

一、Vlayout attach

Vlayout是一种适用于Android的布局框架,它能够方便的实现通用的复杂布局,它的主要特点是支持嵌套、支持异步布局加载,并且提供了Flow、GridLayout、StaggeredGridLayout、LinearLayout和FrameLayout这几种常见的布局方式。 Vlayout attach是Vlayout的核心类之一,它实现了RecyclerView的Layout Manager,同时支持Item复用、异步布局加载和多层布局嵌套。为了实现这个特性,需要在item.xml中添加一个vlayout属性,并且将VlayoutManager设置给RecyclerView。

RecyclerView recyclerView = findViewById(R.id.recycler_view);
VlayoutManager layoutManager = new VlayoutManager();
recyclerView.setLayoutManager(layoutManager);

二、Playoutdoors

Playoutdoors是Vlayout的一个常用Component,它可以用来实现复杂布局。 Playoutdoors是使用Vlayout实现复杂布局的关键,在这里面可以自由的组合多种布局方式,使得它可以与任何View混合在一起,同时可以应用于RecyclerView中。

//添加头部
DelegateAdapter adapter = new DelegateAdapter(layoutManager, true);
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_title));
//添加中间内容
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_goods));
//添加底部
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_ad));
recyclerView.setAdapter(adapter);

三、Vlayout二级列表

Vlayout可以实现二级列表,不仅可以垂直放置,也可以横向放置。它可以嵌套多个Vlayout实现很复杂的布局。

//初始化数据
List<Pair<String, List<String>>> dataList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    List<String> childList = new ArrayList<>();
    for (int j = 0; j < 10; j++) {
        childList.add("我的宝贝" + j);
    }
    dataList.add(new Pair<>("分类" + i, childList));
}
//嵌套两个Vlayout实现二级列表
DelegateAdapter adapter = new DelegateAdapter(layoutManager, false);
//Vlayout1实现垂直列表
adapter.addAdapter(new CategoryAdapter(dataList));
//Vlayout2实现横向瀑布流
adapter.addAdapter(new DelegateAdapter(layoutManager)
        .addAdapter(new HotGoodsAdapter(this)));
recyclerView.setAdapter(adapter);

四、PCB Layout

在使用Vlayout进行布局时,我们一般采用LinearLayout来实现最基本的列表布局,而PCB Layout与LinearLayout非常相似,但是比LinearLayout更加灵活,可以实现更多种复杂的布局方式。 PCB Layout最常用的属性是paddding和margin,这两个属性可以将控件放置在一个大的布局中,从而使得整个界面显得非常整洁。

<com.alibaba.android.vlayout.ext.pclinearlayout
    android:id="@+id/ll_example"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingleft="16dp"
    android:paddingright="16dp"
    android:paddingtop="8dp"
    pcb:pcb_v_space="8dp"
    pcb:pcb_h_space="16dp"
    pcb:pcb_type="h"
    pcb:pcb_auto_height="true" />

五、Pla有透明的吗

Pla(out)是一款全新的异步加载图片框架,能够高效地实现图片的加载和缓存。它支持动态配置Options,并且可以将图片加载到指定的控件中,同时它还支持圆角、带边框、高斯模糊、特殊形状等各种高级需求。 Pla的图片也可以设置透明,只需要在代码中实现对应的Options即可。

//在代码中使用Options设置图片透明度
Options options = new Options();
options.setImageTransparency(0.5f);
image.load("http://example.com/image.jpg", options);

六、Playoutside

Playoutside是Vlayout中一个常用的组件,它可以在加载item之前,将其从页面外加载进来,以达到加速加载的效果。同时它还可以优化Item复用的效果,避免出现大量的重复加载问题。

//使用Playoutside提高布局效率
DelegateAdapter adapter = new DelegateAdapter(layoutManager, true);
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_title));
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_goods).addVvItemView(new Playoutside()));
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_ad).addVvItemView(new Playoutside()));
recyclerView.setAdapter(adapter);

七、Playout下载

Vlayout提供了Playout下载功能,它可以适用于各种场景下异步图片加载的效果。这种方式提供了一种更加高效的图片加载方式,可以实现更好的用户体验。

//使用Playout下载图片
DelegateAdapter adapter = new DelegateAdapter(layoutManager, true);
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_title));
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_goods)
        .addVvItemView(new PlaVout()).addClick(this::openGoodsDetail));
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_ad).addVvItemView(new PlaGift()));
recyclerView.setAdapter(adapter);

八、Layout

Vlayout提供的Layout可以轻松构建复杂布局,它支持嵌套和复合布局,可以应用于Android系统中的各种场景下。

//使用Vlayout实现复杂布局
RecyclerView recyclerView = findViewById(R.id.recycler_view);
VlayoutManager layoutManager = new VlayoutManager();
recyclerView.setLayoutManager(layoutManager);
DelegateAdapter adapter = new DelegateAdapter(layoutManager, true);
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_title));
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_goods)
        .addVvItemView(new PlaVout()).addClick(this::openGoodsDetail));
adapter.addAdapter(new SingleLayoutAdapter(this, R.layout.item_ad).addVvItemView(new PlaGift()));
recyclerView.setAdapter(adapter);

九、Playoutdoors中文翻译

Playoutdoors可以直接翻译为“布局门户”,它能够实现不同布局之间的互通,是Vlayout中最为重要的一个组件之一。

总结

Vlayout是一种非常通用的Android布局框架,它可以用来实现复杂布局和异步加载图片,同时还支持二级列表、PCB Layout、Playoutside和Playout下载等多种常用组件。使用Vlayout可以轻松地构建出适用于各种场景的复杂布局,是Android开发非常值得掌握的技术。