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. hdu2482 字典树+spfa

    题意:       给你一个地图,地图上有公交站点和路线,问你从起点到终点至少要换多少次公交路线. 思路:       首先上面的题意说的和笼统,没说详细是因为这个题目叙述的很多,描述起来麻烦, 下面 ...

  2. hdu5056(找相同字母不出现k次的子串个数)

    题意:      给你一个字符串,然后问你这个字符串里面有多少个满足要求的子串,要求是每个子串相同字母出现的次数不能超过k. 思路:      这种题目做着比较有意思,而且不是很难(但自己还是嘚瑟,w ...

  3. Tomcat反序列化漏洞(CVE-2016-8735)

    目录​​​​​​​ CVE-2016-8735 漏洞复现 漏洞修复: CVE-2016-8735 漏洞描述: 该漏洞与之前Oracle发布的 mxRemoteLifecycleListener 反序列 ...

  4. Windows核心编程 第四章 进程(中)

    4.2 CreateProcess函数 可以用C r e a t e P r o c e s s函数创建一个进程: BOOL CreateProcessW( _In_opt_ LPCWSTR lpAp ...

  5. 支付宝手机端网页支付 PHP(基于官方提供的demo)

    1.支付宝开放平台添加应用并且签约快捷手机wap支付(应用添加不做详细说明) 2.下载demo,文档中心SDK&Demo, 3.个人中心秘钥管理,查看商户appID,商户私钥,支付宝公钥,商户 ...

  6. 在局域网内知道计算机的名字查找计算机的IP

    第一步 nbtstat -a 计算机名字 第二步 nbtstat -c 可以看到计算机地址

  7. vuex、localStorage、sessionStorage之间的区别

    vuex存储在内存中,localStorage以文件形式存储在本地,sessionStorage针对一个session(阶段)进行数据存储. 当页面刷新时vuex存储的数据会被清除,localStor ...

  8. C#读写内置类型的数据时是否原子操作

    Reads and writes of the following data types are atomic: bool, char, byte, sbyte, short, ushort, uin ...

  9. [bug] python3 pip 安装 MarkupSafe==1.0 失败:ImportError:cannot import name 'Feature' from 'setpools'

    解决 先升级pip到最新版本 python -m pip install --upgrade pip 再升级setuptools pip install --upgrade pip setuptool ...

  10. [bug] Nginx:src/os/unix/ngx_user.c:36:7: 错误:‘struct crypt_data’没有名为‘current_salt’的成员

    参考 https://blog.csdn.net/yu_pan_love_cat/article/details/103035513 https://www.cnblogs.com/hxlinux/p ...