Recyclerview的多类型item入门

Recyclerview的多类型item入门

  • 添加依赖库
compile 'com.android.support:recyclerview-v7:25.3.1'
适配器

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.junruiyun.superman.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Smile on 2017/6/1.
 */

public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    /*变量*/
    private List<String> list=new ArrayList<>();
    /*子项类型标记*/
    public static final int TYPE_ORDER_HEAD=0xff01;
    public static final int TYPE_ORDER_BODY=0xff02;

    /*构造函数*/
    public OrderAdapter(List list){
        this.list=list;
    }

    //ViewHold指定对应的布局xml,用于onCreateViewHolder方法

    class HolderTypeHead extends RecyclerView.ViewHolder{
        TextView textView;
        public HolderTypeHead(View itemView){
            super(itemView);//这个super方法写出来后报错才消失
            textView=(TextView)itemView.findViewById(R.id.head_item);
        }

    }
    class HolderTypeBody extends RecyclerView.ViewHolder{
        TextView textView;
        public HolderTypeBody(View itemView){
            super(itemView);
            textView=(TextView) itemView.findViewById(R.id.body_item);
        }
    }

    /**
     * 创建子项布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case TYPE_ORDER_HEAD:
                return new HolderTypeHead(LayoutInflater.from(parent.getContext()).inflate(R.layout.order_recycler_view_head_item,parent,false));
            case TYPE_ORDER_BODY:
                return new HolderTypeBody(LayoutInflater.from(parent.getContext()).inflate(R.layout.order_recycler_view_body_item,parent,false));
            default:
                return null;
        }
    }

    /**
     *绑定子项布局
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HolderTypeHead){
            bindTypehead((HolderTypeHead) holder,position);
        }else if (holder instanceof  HolderTypeBody){
            bindTypeBody((HolderTypeBody) holder,position);
        }
    }

    /**
     * 设定类型
     * @param position
     * @return
     */

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return TYPE_ORDER_HEAD;
        }else{
            return TYPE_ORDER_BODY;
        }
    }

    /**
     * 根据遍历的类型进行指定显示几行
     * @param recyclerView
     */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager=recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager){
        final GridLayoutManager gridLayoutManager=(GridLayoutManager)manager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {//回调一个方法
                @Override
                public int getSpanSize(int position) {
                    int TYPE=getItemViewType(position);
                    switch (TYPE){
                        case TYPE_ORDER_HEAD:
                            return gridLayoutManager.getSpanCount();//传递的是几就显示几列

                        case  TYPE_ORDER_BODY:
                            return 1;//公式:外面设定的数=每行显示的item条数(*)返回数
                        default:
                            return gridLayoutManager.getSpanCount();

                    }
                }
            });
        }
    }

    /**
     * 指定控件的长度
     * @return
     */
    @Override
    public int getItemCount() {
        return  list.size()+1;
    }

    /对应的ViewHold设置(参数使用下面的类)用于onBindViewHolder方法

    /**
     * 头部ViewHold
     */
    private void bindTypehead(HolderTypeHead holder,int position){
        holder.textView.setText("红名列表");

    }

    /**
     * 身体ViewHold
     */
    private void bindTypeBody(HolderTypeBody holder,int position){
        String text=list.get(position-1);
        holder.textView.setText(text);//这个holder就一个TextView
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值