RecyclerView实现复杂多类型布局,再也不用嵌套了

1、RecyclerView 控件通过 setLayoutManager() 方法来设置布局管理器,这个布局管理器分三种 LinearLayoutManagerGridLayoutManagerStaggeredGridLayoutManager

spanCount:在创建 GridLayoutManager 对象的时候构造方法需要传入这个参数,也就是设置每行排列 item 个数。

spanSize:在 setSpanSizeLookup() 方法中,这个方法返回的是当前位置的 item 跨度大小,总跨度(也就是总列数)可以设置,假设总列数设置为6,当getSpanSize设置为2,说明这个item占了6份中的两份,说明有3列, 如果getSpanSize返回3,那么一个item占6份中的3份,说明有两列,如下图:

  

2、有了上面概念, 那我们来实现一个如下图的列表:

首先我们要明确,总列数为4,此列表分为3种类型,这三种类型分别占一列、两列、4列,我们要写3种布局和3个ViewHolder,那么“为你推荐”和"便民生活"这四个字的一行getSpanSize应该返回4,他要把4列都占完,也就是它占整个一列,下面生活缴费、蚂蚁森林等要返回2, 每个item跨度为2,也就是只有两列, 最下面充值中心、信用卡还款等getSpanSize应该返回1,每个item跨度只有1列,因此它有4列。

mLayoutManager = new GridLayoutManager(this, 4);

            mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                ApplicationBean applicationBean = mDataList.get(position);
                if (applicationBean.getType() == ApplicationBean.Type.TypeOne) {
                    return 4;
                } else if (applicationBean.getType() == ApplicationBean.Type.TypeTwo) {
                    return 2;
                } else if (applicationBean.getType() == ApplicationBean.Type.TypeThree) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new ApplicationAdapter(mDataList, this);
        mRecyclerView.setAdapter(mAdapter);
public class ApplicationAdapter extends RecyclerView.Adapter {
    //布局类型
    private static final int VIEW_TYPE_ONE = 1;
    private static final int VIEW_TYPE_TWO = 2;
    private static final int VIEW_TYPE_THREE = 3;
    private LayoutInflater inflater;
    private Context mContext;
    private ArrayList<ApplicationBean> mDataList;

    public ApplicationAdapter(ArrayList<ApplicationBean> mDataList, Context context) {
        this.mDataList = mDataList;
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemViewType(int position) {

        if (mDataList.get(position).getType() == ApplicationBean.Type.TypeOne) {
            return VIEW_TYPE_ONE;
        } else if (mDataList.get(position).getType() == ApplicationBean.Type.TypeTwo) {
            return VIEW_TYPE_TWO;
        } else if (mDataList.get(position).getType() == ApplicationBean.Type.TypeThree) {
            return VIEW_TYPE_THREE;
        } else {
            return 0;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case VIEW_TYPE_ONE:
                viewHolder = new ViewHolderOne(inflater.inflate(R.layout.item_one, parent, false));
                break;
            case VIEW_TYPE_TWO:
                viewHolder = new ViewHolderTwo(inflater.inflate(R.layout.item_two, parent, false));
                break;
            case VIEW_TYPE_THREE:
                viewHolder = new ViewHolderThree(inflater.inflate(R.layout.item_three, parent, false));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        switch (getItemViewType(position)) {
            case VIEW_TYPE_ONE:
                Log.e("huangjie", mDataList.get(position).getTitle());
                ((ViewHolderOne) holder).title.setText(mDataList.get(position).getTitle());
                break;
            case VIEW_TYPE_TWO:
                ((ViewHolderTwo) holder).title.setText(mDataList.get(position).getName());
                ((ViewHolderTwo) holder).information.setText(mDataList.get(position).getInformation());
                ((ViewHolderTwo) holder).icon.setImageDrawable(
                        mContext.getResources().getDrawable(mDataList.get(position).getIcon()));
                break;
            case VIEW_TYPE_THREE:
                ((ViewHolderThree) holder).title.setText(mDataList.get(position).getName());
                ((ViewHolderThree) holder).icon.setImageDrawable(
                        mContext.getResources().getDrawable(mDataList.get(position).getIcon()));

                break;
        }
    }

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


    /**
     * 第一种布局类型ViewHolder
     */
    public static class ViewHolderOne extends RecyclerView.ViewHolder {
        private TextView title;

        public ViewHolderOne(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.text);
        }
    }

    /**
     * 第二种布局类型ViewHolder
     */
    public static class ViewHolderTwo extends RecyclerView.ViewHolder {
        private ImageView icon;
        private TextView title;
        private TextView information;

        public ViewHolderTwo(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.image);
            title = (TextView) itemView.findViewById(R.id.title);
            information = (TextView) itemView.findViewById(R.id.infor);
        }
    }

    /**
     * 第三种布局类型viewholder
     */
    public static class ViewHolderThree extends RecyclerView.ViewHolder {
        private ImageView icon;
        private TextView title;

        public ViewHolderThree(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.image);
            title = (TextView) itemView.findViewById(R.id.title);
        }
    }
}

 

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值