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. 路由器逆向分析------QEMU的下载和安装(Linux平台)

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/68953160 一.QEMU源码的下载和编译 QEMU源码的github下载地址:h ...

  2. LA3135简单多路归并(优先队列)

    题意:       有N个任务,每个任务都有自己的时间间隔(就是每t秒请求执行一次)和任务id,这n个任务公用一个cpu,每次我们都执行时间靠前的,如果相同时间内有多个任务,就执行任务id小的,要求模 ...

  3. svchost服务(DLL服务)

    相比于exe服务,DLL服务只需要一个dll,而且运行是通过svchost.exe来运行的,同时安装和卸载的时候需要自己手动修改相关注册表.原理及其细节就不多说了,直接上代码吧(我写的这个是创建新组然 ...

  4. NSIS制作安装包笔记(一):NSIS介绍、使用NSIS默认向导脚本制作Windows安装包

    前言   做产品时,定制的自定义安装界面常有的,使用NSIS + Qt可以完美的定制基于QT的安装界面,先从纯NSIS开始,制作常规的安装包.   应用程序的发布方式   应用程序发布的时候,具备以下 ...

  5. Day002 Java开发环境搭建

    Java开发环境搭建 JDK.JRE.JVM JDK: Java Development Kit(包涵JRE) JRE: Java Runtime Environment(包涵JVM) JVM: Ja ...

  6. SQL注入,PreparedStatement和Statement

    代码区 还是一个工具类 代码: package cn.itcats.jdbc; import java.sql.Connection;import java.sql.DriverManager;imp ...

  7. 【实用小技巧】spring springmvc集成shiro时报 No bean named 'shiroFilter' available

    查了网上的,很多情况,不同的解决办法,总归一点就是配置文件加载的问题. 先看下配置文件中的配置 web.xml中的主要配置(这是修改后不在报错的:仅仅修改了一个位置:[classpath:spring ...

  8. [刷题] 17 Letter Combinations of a Phone Number

    要求 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合 1 不对应任何字母    示例 输入:"23" 输出:["ad", "ae&q ...

  9. /etc/ssh/sshd_config ssh自动断 cent7

    vim /etc/ssh/sshd_config ClientAliveInterval 60ClientAliveCountMax 8630000 ClientAliveInterval 30Cli ...

  10. Python基础 小白[7天]入门笔记

    笔记来源 Day-1 基础知识(注释.输入.输出.循环.数据类型.随机数) #-*- codeing = utf-8 -*- #@Time : 2020/7/11 11:38 #@Author : H ...