阿里的开源框架_V-layout的使用

VirtualLayout的是阿里巴巴的开源框架,作用是对recyclerview进行功能的扩展,将布局分为不同的区域,每个区域由独立的布局manager进行管理,从而实现在recyclerview中实现同时展示不同的布局效果

导包

    //v-layout
    compile ('com.alibaba.android:vlayout:1.0.3@aar') {
        transitive = true
    }

除adapter外其余用法与recyclerview相同

//继承于DelegateAdapter.Adapter<VH>
public class MyVlayoutAdapter extends DelegateAdapter.Adapter<MyVlayoutAdapter.Holder> {
    private Context context;
    LayoutHelper layoutHelper;
    RecyclerView.LayoutParams layoutParams;
    int count = 0;
    List<String> datas;
    public MyVlayoutAdapter(Context context, LayoutHelper layoutHelper, List<String> datas) {
        this(context, layoutHelper, datas.size(), new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300));
    }

    public MyVlayoutAdapter(Context context, LayoutHelper layoutHelper, int count, RecyclerView.LayoutParams layoutParams) {
        this.context = context;
        this.layoutHelper = layoutHelper;
        this.layoutParams = layoutParams;
        this.count = count;
    }

    //与普通的adapter的唯一的区别在这里,需要在这里返回helper对象
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return layoutHelper;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_item, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        holder.iv.setImageResource(R.mipmap.ic_launcher);
        holder.tv.setText("丫二咯"+position);
        holder.tv.setTextColor(Color.BLACK);
    }

    @Override
    public int getItemCount() {
        return count;
    }
//Holder类,用法与recyclerview相同
    public class Holder extends RecyclerView.ViewHolder {
        ImageView iv;
        TextView tv;

        public Holder(View itemView) {
            super(itemView);
            setView(itemView);
        }

        public void setView(View view) {
            iv = view.findViewById(R.id.tab_imageview);
            tv = view.findViewById(R.id.tab_textview);
        }
    }
}

使用方法

/*      公共属性
*       linearLayoutHelper.setItemCount(4);// 设置布局里Item个数,当与adapter中getItemCount不同时由adapter决定
        linearLayoutHelper.setPadding(10,10,10,10);// 设置LayoutHelper的子元素相对LayoutHelper边缘的距离
        linearLayoutHelper.setMargin(10,10,10,10);// 设置LayoutHelper边缘相对父控件(即RecyclerView)的距离
        linearLayoutHelper.setBgColor(Color.GRAY);// 设置背景颜色
        linearLayoutHelper.setAspectRatio(6);// 设置设置布局内每行布局的宽与高的比
* */
        //绑定vlayoutmanager
        VirtualLayoutManager vlayout = new VirtualLayoutManager(this);
        rv.setLayoutManager(vlayout);


        //helper对象,管理不同区域间的布局-------------------------------------------------------------------------------------------
        //线性
        LinearLayoutHelper linearLayoutHelper = new LinearLayoutHelper();
        linearLayoutHelper.setDividerHeight(2);
        linearLayoutHelper.setMarginLeft(8);
        MyVlayoutAdapter adapter = new MyVlayoutAdapter(this, linearLayoutHelper, datas);

        //格子
        GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(4);
        gridLayoutHelper.setAutoExpand(false);
        //设置每行的item的个数
        gridLayoutHelper.setSpanCount(4);
        gridLayoutHelper.setMargin(10,10,10,10);
        gridLayoutHelper.setWeights(new float[]{30, 10, 30, 30});
        gridLayoutHelper.setSpanSizeLookup(new GridLayoutHelper.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //设置每个item占用多少个item
                if (position < 25) {
                    return 4;
                } else if (position < 50) {
                    return 2;
                } else {
                    return 1;
                }
            }
        });
        MyVlayoutAdapter adapter2 = new MyVlayoutAdapter(this, gridLayoutHelper, datas);

        //固定,只能显示一个
        FixLayoutHelper fixLayoutHelper = new FixLayoutHelper(FixLayoutHelper.TOP_LEFT, 0, 0);
        MyVlayoutAdapter adapter3 = new MyVlayoutAdapter(this, fixLayoutHelper, datas);

        //固定布局,可以通过动作来设置是否显示,与FixLayoutHelper冲突
        //SHOW_ALWAYS:与FixLayoutHelper的行为一致,固定在某个位置
        //SHOW_ON_ENTER:默认不显示视图,当页面滚动到这个视图的位置的时候,才显示;
        //SHOW_ON_LEAVE:默认不显示视图,当页面滚出这个视图的位置的时候显示
        ScrollFixLayoutHelper scrollFixLayoutHelper = new ScrollFixLayoutHelper(ScrollFixLayoutHelper.TOP_RIGHT, 0, 0);
        scrollFixLayoutHelper.setShowType(ScrollFixLayoutHelper.SHOW_ALWAYS);
        MyVlayoutAdapter adapter4 = new MyVlayoutAdapter(this, scrollFixLayoutHelper, datas);

        //设置浮动布局,FloatLayoutHelper设置的Item必须要在屏幕滚动到它那个实际原始位置,加载之后才能被拖动
        FloatLayoutHelper floatLayoutHelper = new FloatLayoutHelper();
        floatLayoutHelper.setDefaultLocation(20, 250);
        MyVlayoutAdapter adapter5 = new MyVlayoutAdapter(this, floatLayoutHelper, datas);

        //栏格布局,就是只有一栏的布局,这一栏可以设置多个Item,但是需要有对应的Weight属性
        ColumnLayoutHelper columnLayoutHelper = new ColumnLayoutHelper();
        columnLayoutHelper.setItemCount(5);
        //总值为100
        columnLayoutHelper.setWeights(new float[]{30, 10, 30, 20, 10});
        MyVlayoutAdapter adapter6 = new MyVlayoutAdapter(this, columnLayoutHelper, datas.subList(0, 5));


        //单独的一个item
        SingleLayoutHelper singleLayoutHelper = new SingleLayoutHelper();
        singleLayoutHelper.setItemCount(3);
        MyVlayoutAdapter adapter7 = new MyVlayoutAdapter(this, singleLayoutHelper, datas.subList(0, 5));

        /**OnePlusNLayoutHelper,核心布局
         * 1 + 0                        1 + 1
         * -------------------------    -------------------------
         * |                       |    |           |           |
         * |                       |    |           |           |
         * |           1           |    |           |           |
         * |                       |    |     1     |     2     |
         * |                       |    |           |           |
         * |                       |    |           |           |
         * -------------------------    -------------------------
         *
         *
         * 1 + 2                        1 + 3
         * -------------------------    -------------------------
         * |           |           |    |           |           |
         * |           |     2     |    |           |     2     |
         * |           |           |    |           |           |
         * |     1     |-----------|    |     1     |-----------|
         * |           |           |    |           |     |     |
         * |           |     3     |    |           |  3  |  4  |
         * |           |           |    |           |     |     |
         * -------------------------    -------------------------
         *
         *  1 + 4
         * -------------------------
         * |           |           |
         * |           |     2     |
         * |           |           |
         * |     1     |-----------|
         * |           |   |   |   |
         * |           | 3 | 4 | 5 |
         * |           |   |   |   |
         * -------------------------
         */
        OnePlusNLayoutHelper onePlusNLayoutHelper = new OnePlusNLayoutHelper(4);
        MyVlayoutAdapter adapter8 = new MyVlayoutAdapter(this, onePlusNLayoutHelper, datas.subList(0, 5));

        //设置Sticky布局,stikcy布局,根据stickyStart属性,当视图的位置在屏幕范围内时,
        // 视图会随页面滚动而滚动;当视图的位置滑出屏幕时,StickyLayoutHelper会将视图固定在顶部或底部
        StickyLayoutHelper stickyLayoutHelper = new StickyLayoutHelper();
        stickyLayoutHelper.setStickyStart(true);
        MyVlayoutAdapter adapter9 = new MyVlayoutAdapter(this, stickyLayoutHelper, datas.subList(0, 5));

        //设置瀑布流布局,可以在holder中设置不同的高度
        StaggeredGridLayoutHelper staggeredGridLayoutHelper = new StaggeredGridLayoutHelper();
        staggeredGridLayoutHelper.setLane(3);
        staggeredGridLayoutHelper.setHGap(5);//水平间距
        staggeredGridLayoutHelper.setVGap(5);//垂直间距
        MyVlayoutAdapter adapter10 = new MyVlayoutAdapter(this, staggeredGridLayoutHelper, datas);
        //绑定所有adapter-------------------------------------------------------------------------------------------------------
        List<DelegateAdapter.Adapter> adapters = new ArrayList<>();
//                adapters.add(adapter10);
        //        adapters.add(adapter8);
        //        adapters.add(adapter7);
        //        adapters.add(adapter6);
        //        adapters.add(adapter5);
        //        adapters.add(adapter4);
        //        adapters.add(adapter3);
                adapters.add(adapter2);
        //        adapters.add(adapter9);
        //        adapters.add(adapter);

        //绑定多个adapter,不是通过vlayout直接添加adapters,而是通过DelegateAdapter作为粘合剂连接layoutmanager和adapters,
        //估计是vlayout直接添加adapters实现有难度或耦合性太强
        DelegateAdapter delegateAdapter = new DelegateAdapter(vlayout);
        delegateAdapter.setAdapters(adapters);
        rv.setAdapter(delegateAdapter);
        //在这里通过添加滚动监听判断是否到达底部实现上拉刷新,>>>不好实现,因为不同的布局难以判断recyclerview的总高度是多少
        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值