孟老板 BaseAdapter封装 (二) Healer,footer
- BaseAdapter封装(一) 简单封装
- BaseAdapter封装(二) Header,footer
- BaseAdapter封装(三) 空数据占位图
- BaseAdapter封装(四) PageHelper
- BaseAdapter封装(五) ListAdapter
- BaseAdapter封装(六) Healer,footer for List
- BaseAdapter封装(七) ConcatAdapter 改建头尾
- BaseAdapter封装(八) Paging 分页
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的更多相关文章
- 孟老板 BaseAdapter封装(五) ListAdapter
BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...
- 孟老板 BaseAdapter封装 (一) 简单封装
BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...
- 孟老板 BaseAdapter封装 (三) 空数据占位图
BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...
- 孟老板 BaseAdapter封装(四) PageHelper
BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...
- 孟老板 ListAdapter封装, 告别Adapter代码 (上)
BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...
- 孟老板 ListAdapter封装, 告别Adapter代码 (三)
BaseAdapter系列 ListAdapter封装, 告别Adapter代码 (一) ListAdapter封装, 告别Adapter代码 (二) ListAdapter封装, 告别Adapter ...
- 孟老板 ListAdapter封装, 告别Adapter代码 (四)
BaseAdapter系列 ListAdapter封装, 告别Adapter代码 (一) ListAdapter封装, 告别Adapter代码 (二) ListAdapter封装, 告别Adapter ...
- 孟老板 Paging3 (二) 结合Room
BaseAdapter系列 ListAdapter系列 Paging3 (一) 入门 Paging3 (二) 结合 Room Paging3 (二) 结合Room Paging 数据源不开放, 无法 ...
- 孟老板 Paging3 (一) 入门
BaseAdapter系列 ListAdapter系列 Paging3 (一) 入门 Paging3 (二) 结合 Room Paging3 (一) 入门 前言: 官方分页工具, 确实香. 但 ...
随机推荐
- POJ 2976 01分数规划基础题目
题意: 给你一组"数",一共n个,每个数有两个权值,价钱a[i],代价b[i],让你选择n - k使得 sigma(a[i]) / sigma(b[i]) * 100 ...
- 修改Android手机内核,绕过反调试
本文博客链接:http://blog.csdn.net/qq1084283172/article/details/57086486 0x1.手机设备环境 Model number: Nexus 5 O ...
- UVA10970大块巧克力
题意: 题意,给你一块n*m的巧克力,最终是要把他切成n*m快小蛋糕,问最小切多少刀?每一刀只能把一个整体切成两个整体,不可以把两个整体分成四个整体,就是说只能切一个地方. 思路: ...
- PKI/CA与证书服务
目录 PKI CA RA LDAP目录服务 CRL证书作废系统 数字证书 证书验证 证书撤销 证书更新 PKI系统的构成 PKI PKI(Public Key Infrastructure)公钥基础设 ...
- 在 Peach 中使用发布者进行调试
0x01 桃子平台 桃子平台(Peach)是一款流行的 Fuzz 平台,主要用作二进制文件及网络协议的模糊测试.其原理遵循基本的模糊测试流程,比较有特色的是它依赖用户所编写的 Pit 文件,同时输入的 ...
- 删除自定义服务 寒江孤钓<<windows 内核安全编程>> 学习笔记
书中第一章 成功启动first服务之后, 发现书中并没有介绍删除服务的方式, SRVINSTW工具中的移除服务功能,也无法找到我们要删除的服务, 于是,搜素了下,发现解决方法如下: 在cmd中输入 & ...
- 【JavaScript】JS从入门到深入(复习查漏向
[JavaScript]JS从入门到深入(复习查漏向 pre 精细得学过一遍JS后才发现,原来之前CTF中有些nodejs的题目以及一些游戏题的payload就变得很好理解了. 基础知识 ECMASc ...
- Canal详细入门实战(使用总结)
Canal介绍 Canal简介 canal [kə'næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费 早期阿里巴巴因为杭州和美国双机房部署,存在 ...
- Linux主机USB RNDIS网卡驱动实现不完整导致的一例问题
某通信模块设备,通过USB提供RDNIS和ECM网卡功能.在实际应用中发现,USB RNDIS网卡模式下,当使用AT指令以不同的CID拨号的时候,在Windows主机上能正常拨号成功,但在Linux主 ...
- 【Web前端HTML5&CSS3】08-盒模型补充
笔记来源:尚硅谷Web前端HTML5&CSS3初学者零基础入门全套完整版 目录 盒模型补充及田径场实战 1. 盒子大小 2. 轮廓 3. 阴影 4. 圆角 圆 椭圆 盒模型补充及田径场实战 1 ...