vlayout的初始尝试使用

项目需求图

vlayout的初始尝试使用_第1张图片
vlayout的初始尝试使用_第2张图片
可以看到,当item个数为奇数的时候,第一个元素要单独占据一行,有点类似于excel中的合并单元格;当item个数为偶数的时候,就当做普通网格展示就好。

对于这种不规则的列表展示方式,vlayout就是最好的选择。由于初次始使用这个,需要先做技术预研,所以就写了个简单的demo体验效果。

效果如下:

(1)当item个数为奇数的时候,展示效果如下:
vlayout的初始尝试使用_第3张图片
(2)当item个数为偶数的时候,展示效果如下:

vlayout的初始尝试使用_第4张图片

代码实现:

(1)网格合并的关键代码:

layoutHelper.setSpanSizeLookup(new GridLayoutHelper.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if(mDatas.size()%2==0){//如果是偶数个数,平均分配,否则合并
                  return 1;
                }else{
                    if(position==0){
                        return 2;
                    }else{
                        return 1;
                    }
                }

            }
        });

(2)其余代码如下:
Activity中代码:

 //查找控件
        mRvRecipe = findViewById(R.id.rv_recipe);

        //初始化LayoutManager
        mLayoutManager = new VirtualLayoutManager(this);
        mRvRecipe.setLayoutManager(mLayoutManager);

        //设置回收复用池大小
        viewPool = new RecyclerView.RecycledViewPool();
        mRvRecipe.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 20);
        mLayoutManager.setRecycleOffset(300);

        //设置item间隔
        RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(4, 4, 4, 4);
            }
        };
        mRvRecipe.addItemDecoration(itemDecoration);

        //设置网格布局
        GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(2);
        gridLayoutHelper.setAutoExpand(true);//是否自动填充空白区域

        //设置Adapter列表 和数据
        mAdapter = new RecipeAdapter(this,gridLayoutHelper);
        List<DelegateAdapter.Adapter> adapters = new LinkedList<>();
        adapters.add(mAdapter) ;

        //设置DelegateAdapter
        DelegateAdapter delegateAdapter = new DelegateAdapter(mLayoutManager);
        delegateAdapter.setAdapters(adapters);

       //设置apapter给recyecleview
        mRvRecipe.setAdapter(delegateAdapter);

Adapter中代码:

public class RecipeAdapter extends DelegateAdapter.Adapter<RecipeAdapter.RecipeViewHolder> {

    private Context mContext;
    private GridLayoutHelper mLayoutHelper;
    private ArrayList<String> mDatas = new ArrayList<>();

    private void initDatas() {
        mDatas.clear();
        for (int i = 0; i <4; i++) {
            mDatas.add("标题" + i);
        }
    }

    public RecipeAdapter(Context context, GridLayoutHelper layoutHelper) {
        mContext = context;
        mLayoutHelper = layoutHelper;
        initDatas();
        layoutHelper.setSpanSizeLookup(new GridLayoutHelper.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if(mDatas.size()%2==0){//如果是偶数个数,平均分配,否则合并
                  return 1;
                }else{
                    if(position==0){
                        return 2;
                    }else{
                        return 1;
                    }
                }
            }
        });
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }


    @Override
    public RecipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecipeViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecipeViewHolder holder, int position) {
        holder.mTvTitle.setText(mDatas.get(position));
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class RecipeViewHolder extends RecyclerView.ViewHolder {
        public TextView mTvTitle;
        public RecipeViewHolder(View itemView) {
            super(itemView);
            mTvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }
}

总结:

单独从使用上来看,还是很好入手的,至于其余复杂用法,代码具体是怎么实现的,留待后面研究。

你可能感兴趣的:(Android控件)