1.添加 Header Footer

前言:

上一遍已经对 Adapter做了简单的封装, 这一遍我们为Adaper 添加头尾View;  
头尾view时比较常见的业务场景. 相信大家也都用过, 来比较一下差别吧.

1.1 分析:

  1. 既然要添加头尾,  就必须要有 头尾对应的 View, 这个View一般从 Activity或Fragment中维护管理;

  2. 头尾View, 应属于不用的 ItemType 类型;

  3. 注意 postion的正确性; header也是一个Item 会让 列表数据的 position + 1

直接上源码:

public abstract class HeadAdapter<T> extends BaseAdapter<T> {
/**
* 头布局
*/
private View mHeadView;
/**
* 尾布局
*/
private View mFootView; /**
* 头尾布局的 itemType
*/
protected static final int ITEM_HEAD = 0xab;
protected static final int ITEM_FOOT = 0xac; public HeadAdapter(@NotNull Context mContext, @Nullable List<T> mData, @Nullable AdapterListener listener) {
super(mContext, mData, listener);
} public View getmHeadView() {
return mHeadView;
} public void setmHeadView(@NotNull View mHeadView) {
//设置 头尾 View时, 刷新布局
notifyItemInserted(0);
this.mHeadView = mHeadView;
} public View getmFootView() {
return mFootView;
} public void setmFootView(@NotNull View mFootView) {
notifyItemInserted(super.getItemCount() + getHeadCount());
this.mFootView = mFootView;
} /**
* 获取条目类型. 校验是否 头尾 View
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
if(checkHead(position)) return ITEM_HEAD;
if(checkFoot(position)) return ITEM_HEAD;
return getMyType(position);
} @NotNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
// 头尾 View 时 返回 EndHolder
if(viewType == ITEM_HEAD && hasHead()) return new EndHolder(mHeadView);
if(viewType == ITEM_FOOT && hasFoot()) return new EndHolder(mFootView);
return createMyHolder(parent, viewType);
} @Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recHolder, int position) {
//头尾 View 不需要额外渲染数据; 真是条目的 position 需要减去 header 长度;
if (checkHead(position)) return;
if (checkFoot(position)) return;
super.onBindViewHolder(recHolder, position - getHeadCount());
} /**
* 条目数量; 头尾数量 + 集合数量
* @return
*/
@Override
public int getItemCount() {
return super.getItemCount() + getHeadCount() + getFootCount();
} /**
* 获取头部view条目数量
* @return 返回头部view条目数量
*/
public int getHeadCount(){
if (mHeadView == null){
return 0;
}
return 1;
} /**
* 获取foot view条目数量
* @return 返回foot view条目数量
*/
public int getFootCount(){
if (mFootView == null){
return 0;
}
return 1;
} /**
* 是否包含 HeadView
*/
public boolean hasHead(){
return mHeadView != null;
} /**
* 是否包含 FootView
*/
public boolean hasFoot(){
return mHeadView != null;
} /**
* 判断条目是否为 HeadView
* @param position 条目索引
* @return
*/
private boolean checkHead(int position) {
return position == 0 && mHeadView != null;
} /**
* 判断条目是否为 FootView
* @param position 条目索引
* @return
*/
private boolean checkFoot(int position) {
return position >= super.getItemCount() + getHeadCount();
} private static class EndHolder extends RecyclerView.ViewHolder{
public EndHolder(@NonNull View itemView) {
super(itemView);
}
}

回到顶部

孟老板 BaseAdapter封装 (二) Healer,footer的更多相关文章

  1. 孟老板 BaseAdapter封装(五) ListAdapter

    BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...

  2. 孟老板 BaseAdapter封装 (一) 简单封装

    BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...

  3. 孟老板 BaseAdapter封装 (三) 空数据占位图

    BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...

  4. 孟老板 BaseAdapter封装(四) PageHelper

    BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...

  5. 孟老板 ListAdapter封装, 告别Adapter代码 (上)

    BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...

  6. 孟老板 ListAdapter封装, 告别Adapter代码 (三)

    BaseAdapter系列 ListAdapter封装, 告别Adapter代码 (一) ListAdapter封装, 告别Adapter代码 (二) ListAdapter封装, 告别Adapter ...

  7. 孟老板 ListAdapter封装, 告别Adapter代码 (四)

    BaseAdapter系列 ListAdapter封装, 告别Adapter代码 (一) ListAdapter封装, 告别Adapter代码 (二) ListAdapter封装, 告别Adapter ...

  8. 孟老板 Paging3 (二) 结合Room

    BaseAdapter系列 ListAdapter系列 Paging3 (一) 入门 Paging3 (二) 结合 Room Paging3 (二)  结合Room Paging 数据源不开放, 无法 ...

  9. 孟老板 Paging3 (一) 入门

    BaseAdapter系列 ListAdapter系列 Paging3 (一) 入门 Paging3 (二) 结合 Room Paging3 (一)  入门 前言: 官方分页工具,  确实香.   但 ...

随机推荐

  1. hdu1251 hash或者字典树

    题意: 统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量 ...

  2. hdu3715 二分+2sat+建图

    题意:       给你一个递归公式,每多一层就多一个限制,问你最多能递归多少层. 思路:      先分析每一层的限制 x[a[i]] + x[b[i]] != c[i],这里面x[] = 0,1, ...

  3. 路由器逆向分析------MIPS交叉编译环境的搭建(Buildroot)

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/68950682 为了能在我们熟悉的windows或者ubuntu下开发mips架构的 ...

  4. json对象的获取

    <script type="text/javascript"> var person = { //json对象定义开始 name:'tom', //字符串 age:24 ...

  5. 双非硕士的辛酸求职回忆录:第 2 篇 谈谈我是如何同时找到Java、Python、Go等开发岗和国企银行的科技岗位Offer(上篇)

    1. 双非硕士的辛酸求职之旅--谈谈我是如何同时找到Java.Python.Go等开发岗和国企银行的offer 1.1. 秋招最终情况 本人情况:双非硕,意向工作城市广深,Java和Python技术栈 ...

  6. Qt链接MySQL发布后Drive not loadedDrive not loaded怎么办

    缺少动态链接库!把MySQL文件夹下面带dll.lib的全复制进去就行了

  7. 【实用小技巧】spring与springmvc自动扫描包重复的问题解决

    spring对应配置文件为: <!-- 配置自动扫描的包,此时要排除Controller --> <context:component-scan base-package=" ...

  8. myysql 不能远程访问的解决办法

    1.通过navicat或者命令行,将user表中原来host=localhost的改为host=% 命令行方式: mysql> update user set host = '%' where ...

  9. c#私钥加密统一JAVA

    public static string RSADecryptByPavKey(string pavKey,string strEncryptString) { string clearText = ...

  10. Java安全之Filter权限绕过

    Java安全之Filter权限绕过 0x00 前言 在一些需要挖掘一些无条件RCE中,大部分类似于一些系统大部分地方都做了权限控制的,而这时候想要利用权限绕过就显得格外重要.在此来学习一波权限绕过的思 ...