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. ARM汇编中的:比较指令--CMN / CMP / TEQ / TST

    1. 简介 CMP / CMN : 算术指令 TEQ / TST : 逻辑指令      它们总是会影响CPSR条件标志位. APSR(CPSR)与condition的关系图: 2. CMN -- 比 ...

  2. hdu3472 混合欧拉

    题意:       给你一些字符串,有的字符串反过来也有意义,题目问给的这n个字符串是否可以首尾相连,组成一个串. 思路:       算是混合欧拉的基础题目了,混合欧拉就是专门处理这类问题的,先说下 ...

  3. GNU C++的符号改编机制介绍(函数的名称粉碎格式解析)

    转载:http://blog.csdn.net/roland_sun/article/details/43233565 众所周知,强大的C++相较于C增添了许多功能.这其中就包括类.命名空间和重载这些 ...

  4. 后渗透阶段之基于MSF的内网主机探测

    当我们通过代理可以进入某内网,需要对内网主机的服务进行探测.我们就可以使用MSF里面的内网主机探测模块了. 在这之前,先修改 /etc/proxychains.conf ,加入我们的代理. 然后 pr ...

  5. IDEA只有Commit没有Push按钮

    问题描述 idea的右上角只有commit按钮,而没有push按钮 问题解决 打开File->Settings->Menus and Toolbars->Navigation Bar ...

  6. IDEA安装插件时搜索不到,一直在转圈刷新,无法安装

    方法一:更换一个网络(我自己没有测试过) 参考链接:https://blog.csdn.net/m0_37856386/article/details/110389028 方法二:打开settings ...

  7. DVWA--SQL Injection

    sql注入是危害比较大的一种漏洞,登录数据库可以进行文件上传,敏感信息获取等等. Low 先来看一下源码 <?php if( isset( $_REQUEST[ 'Submit' ] ) ) { ...

  8. jQuery的入口和jQurey的对象切换

    jQuery jQuery的导入 通过script标签的src属性,link标签是导入层叠样式表 jQuery和原生JS的入口函数 1.jQ在页面结构加载完毕就会执行 原生JS的入口函数是等图片.层叠 ...

  9. [bug] Authentication failed for token submission (认证失败)异常

    原因 gitee上下的项目,启动后能访问首页,但登录报错.原因是根据用户名上数据库查密码没有得到结果,中间任何环节有问题都可能导致,我的是因为mapper.xml中的<mapper namesp ...

  10. 进入除错模式!进入此模式后,将会出现更多的选项,分别是: · 以基本图形介面安装 CentOS 7 (使用标准显卡来设定安装流程图示); · 救援Centos系统; · 执行内存测试(Run a memory test);

    Centos 7.3 安装 0.0392017.07.14 20:12:09字数 1550阅读 985 Centos 7.3 基于 Red Hat 企业版的源代码的最新版本的 CentOS 7 在今年 ...