多类型的RecyclerView

1.由于recyclerView没有提供像Listview一样的addheadView的方法,就需要我们自己设置head。
这里写图片描述
布局很简单上下两个头和脚布局。内容写了一些假数据。贴一下adapter中的代码:

public class HeadFootAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    /**
     * ITEM的类型
     */
    private static final int HEAD_TYPE = 0;
    private static final int FOOT_TYPE = 1;
    private static final int CONTENT_TYPE = 2;
    private Context context;
    private List<String> list;
    /**
     * 头布局的数量
     */
    private int HEAD_COUNT = 1;
    /**
     * 脚布局的数量
     */
    private int FOOT_COUNT = 1;

    public HeadFootAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    //判断当前item是否是HeadView
    public boolean isHeaderView(int position) {
        return HEAD_COUNT != 0 && position < HEAD_COUNT;
    }

    //判断当前item是否是FooterView
    public boolean isBottomView(int position) {
        return FOOT_COUNT != 0 && position >= (HEAD_COUNT + list.size());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEAD_TYPE) {
            return new HeadViewHolder(View.inflate(context, R.layout.item_head, null));
        } else if (viewType == FOOT_TYPE) {
            return new FootViewHolder(View.inflate(context, R.layout.item_foot, null));
        } else {
            return new ContentViewHolder(View.inflate(context, R.layout.item_head_foot, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadViewHolder) {//头布局

        } else if (holder instanceof FootViewHolder) {//            脚布局

        } else {
            ((ContentViewHolder) holder).txtContent.setText(list.get(position-1));
        }
    }

    @Override
    public int getItemCount() {
        return HEAD_COUNT + list.size() + FOOT_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        int dataCount = list.size();
        if (HEAD_COUNT != 0 && position < HEAD_COUNT) {
            return HEAD_TYPE;
        } else if (FOOT_COUNT != 0 && position >= (HEAD_COUNT + dataCount)) {
            return FOOT_TYPE;
        } else {
            return CONTENT_TYPE;
        }
    }

/**
 * 内容的VIEWHOLDER
 */
public static class ContentViewHolder extends RecyclerView.ViewHolder {
    public TextView txtContent;

    public ContentViewHolder(View itemView) {
        super(itemView);
        txtContent = get(itemView, R.id.txt_content);
    }

    private static <T extends View> T get(View view, int id) {
        SparseArray<View> viewholder = (SparseArray<View>) view.getTag();
        if (viewholder == null) {
            viewholder = new SparseArray<View>();
            view.setTag(viewholder);
        }
        View chicldView = viewholder.get(id);
        if (chicldView == null) {
            chicldView = view.findViewById(id);
            viewholder.put(id, chicldView);
        }
        return (T) chicldView;
    }
}

/**
 * 头布局的VIEWHOLDER
 */
public static class HeadViewHolder extends RecyclerView.ViewHolder {

    public HeadViewHolder(View itemView) {
        super(itemView);
    }

    private static <T extends View> T get(View view, int id) {
        SparseArray<View> viewholder = (SparseArray<View>) view.getTag();
        if (viewholder == null) {
            viewholder = new SparseArray<View>();
            view.setTag(viewholder);
        }
        View chicldView = viewholder.get(id);
        if (chicldView == null) {
            chicldView = view.findViewById(id);
            viewholder.put(id, chicldView);
        }
        return (T) chicldView;
    }
}

/**
 * 脚布局的VIEWHOLDER
 */
public static class FootViewHolder extends RecyclerView.ViewHolder {

    public FootViewHolder(View itemView) {
        super(itemView);
    }

    private static <T extends View> T get(View view, int id) {
        SparseArray<View> viewholder = (SparseArray<View>) view.getTag();
        if (viewholder == null) {
            viewholder = new SparseArray<View>();
            view.setTag(viewholder);
        }
        View chicldView = viewholder.get(id);
        if (chicldView == null) {
            chicldView = view.findViewById(id);
            viewholder.put(id, chicldView);
        }
        return (T) chicldView;
    }
}
}

首先通过getViewType设置item的类型:

@Override
    public int getItemViewType(int position) {
        int dataCount = list.size();
        if (HEAD_COUNT != 0 && position < HEAD_COUNT) {
            return HEAD_TYPE;
        } else if (FOOT_COUNT != 0 && position >= (HEAD_COUNT + dataCount)) {
            return FOOT_TYPE;
        } else {
            return CONTENT_TYPE;
        }
    }

在onCreateViewHolder中通过类型判断设置item的布局:

 @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEAD_TYPE) {
            return new HeadViewHolder(View.inflate(context, R.layout.item_head, null));
        } else if (viewType == FOOT_TYPE) {
            return new FootViewHolder(View.inflate(context, R.layout.item_foot, null));
        } else {
            return new ContentViewHolder(View.inflate(context, R.layout.item_head_foot, null));
        }
    }

最后在Activity中设置Adapter:

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.head_foot_activity);
        headFootRecyclerView = (RecyclerView) findViewById(R.id.headFootRecyclerView);
        initData();
        final GridLayoutManager grid = new GridLayoutManager(this, 4);
        headFootRecyclerView.setLayoutManager(grid);
        final HeadFootAdapter adapter = new HeadFootAdapter(this, list);
        headFootRecyclerView.setAdapter(adapter);
        grid.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {
                return (adapter.isHeaderView(position)||adapter.isBottomView(position))?grid.getSpanCount():1;
            }
        });
    }

在设置头布局的时候为头布局设置spansize设置了占据4格的单元格:

 grid.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {
                return (adapter.isHeaderView(position)||adapter.isBottomView(position))?grid.getSpanCount():1;
            }
        });

通过判断当前位置是否是头布局或脚布局,如果是的话就让头布局占据4格。

(adapter.isHeaderView(position)||adapter.isBottomView(position))?grid.getSpanCount():1;

布局比较简单就不贴了,源码链接:git源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值