vlayout全称是VirtualLayout,是一个针对RecyclerView的LayoutManager扩展。是阿里最近开源的一个库,主要提供一整套布局方案和布局间的组件复用的问题。
compile (‘com.alibaba.android:vlayout:1.0.4@aar’) {
transitive = true
}
2.初始化VirtualLayoutManager
VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(this);
mRecylerView.setLayoutManager(virtualLayoutManager);
3.设置缓存view个数
final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
mRecylerView.setRecycledViewPool(viewPool);
viewPool.setMaxRecycledViews(0, 20);
4.RecylerView创建适配器。初始化DelegateAdapter
adapter = new DelegateAdapter(virtualLayoutManager, false);
//第一行
SingleLayoutHelper singleLayoutHelper = new SingleLayoutHelper();
adapterBanner1 = new AdapterBanner(singleLayoutHelper);
//第二行
GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(5);
adapterTowItem = new AdapterTowItem(gridLayoutHelper, this);
6.添加适配器
//添加适配器到
adapter.addAdapter(adapterBanner1);
adapter.addAdapter(adapterTowItem);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(virtualLayoutManager);