主要布局如下:
LinearLayoutHelper: 线性布局
GridLayoutHelper: Grid布局, 支持横向的colspan
FixLayoutHelper: 固定布局,始终在屏幕固定位置显示
ScrollFixLayoutHelper: 固定布局,但之后当页面滑动到该图片区域才显示, 可以用来做返回顶部或其他书签等
FloatLayoutHelper: 浮动布局,可以固定显示在屏幕上,但用户可以拖拽其位置
ColumnLayoutHelper: 栏格布局,都布局在一排,可以配置不同列之间的宽度比值
SingleLayoutHelper: 通栏布局,只会显示一个组件View
OnePlusNLayoutHelper: 一拖N布局,可以配置1-5个子元素
StickyLayoutHelper: stikcy布局, 可以配置吸顶或者吸底
StaggeredGridLayoutHelper: 瀑布流布局,可配置间隔高度/宽度
示例代码如下:
···
recyclerView = findViewById(R.id.recycler);
VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(this);
RecyclerView.RecycledViewPool recycledViewPool = new RecyclerView.RecycledViewPool();
recycledViewPool.setMaxRecycledViews(0, 10);
recyclerView.setRecycledViewPool(recycledViewPool);
List<LayoutHelper> layoutHelpers = new ArrayList<>();
LinearLayoutHelper linearLayoutHelper = new LinearLayoutHelper(10, 2);
GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(10, 3);
FixLayoutHelper fixLayoutHelper=new FixLayoutHelper(200,200);
ScrollFixLayoutHelper scrollFixLayoutHelper=new ScrollFixLayoutHelper(400,400);
FloatLayoutHelper floatLayoutHelper=new FloatLayoutHelper();
floatLayoutHelper.setDragEnable(true);
ColumnLayoutHelper columnLayoutHelper=new ColumnLayoutHelper();
columnLayoutHelper.setWeights(new float[]{1f,2f,1.2f,5f});
StickyLayoutHelper stickyLayoutHelper=new StickyLayoutHelper(true);
layoutHelpers.add(linearLayoutHelper);
layoutHelpers.add(gridLayoutHelper);
layoutHelpers.add(fixLayoutHelper);
layoutHelpers.add(scrollFixLayoutHelper);
layoutHelpers.add(floatLayoutHelper);
layoutHelpers.add(columnLayoutHelper);
layoutHelpers.add(stickyLayoutHelper);
virtualLayoutManager.setLayoutHelpers(layoutHelpers);
recyclerView.setLayoutManager(virtualLayoutManager);
List<String> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add("第" + i + "个");
}
recyclerView.setAdapter(new MyShiPei(virtualLayoutManager, list));
···
class MyShiPei extends VirtualLayoutAdapter<MyViewHolder> {
private List<String> list;
public MyShiPei(@NonNull VirtualLayoutManager layoutManager, List<String> list) {
super(layoutManager);
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, parent, false));
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText(list.get(position));
holder.imageView.setImageResource(R.mipmap.ic_launcher);
}
@Override
public int getItemCount() {
return list.size();
}
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView;
public MyViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text);
imageView = itemView.findViewById(R.id.image);
}
}
依赖如下:
compile ('com.alibaba.android:vlayout:1.2.8@aar') {
transitive = true
}
网友评论