BRVAH分组功能原理分析
来源:互联网 发布:Linux集群怎么安字体 编辑:程序博客网 时间:2024/05/20 01:46
最近在diycode社区遇到一位同学提问,所以特写此文章来分析BRVAH分组功能的实现。如果还什么疑问都可以在这里进行提问 因为开源项目和技术分享收到 Google 的面试邀请,大家有什么想要讨论的么?
问题分析的步骤:
1. 如何使用
2. 原理分析
如何该框架的分组功能
Adapter:
public class SectionAdapter extends BaseSectionQuickAdapter<MySection> { public SectionAdapter(int layoutResId, int sectionHeadResId, List data) { super(layoutResId, sectionHeadResId, data); } @Override protected void convert(BaseViewHolder helper, MySection item) { helper.setImageUrl(R.id.iv, (String) item.t); } @Override protected void convertHead(BaseViewHolder helper,final MySection item) { helper.setText(R.id.header, item.header); }
adapter的构造需要传入三个参数,分别是内容的布局和头部的布局和数据源,数据源需要继承SectionEntity
如下:
Entity:
public class MySection extends SectionEntity<Video> { public MySection(boolean isHeader, String header, boolean isMroe) { super(isHeader, header); } public MySection(Video t) { super(t); } }
填充数据
public static List<MySection> getSampleData() { List<MySection> list = new ArrayList<>(); list.add(new MySection(true, "Section 1")); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(true, "Section 2")); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(true, "Section 3")); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(true, "Section 4")); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(true, "Section 5")); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); list.add(new MySection(new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, CYM_CHAD))); return list;}
原理分析
其实头部和内容部分就是通过不同的type来实现的,我们可以查看BaseSectionQuickAdapter
源码
@Override protected int getDefItemViewType(int position) { return ((SectionEntity) mData.get(position)).isHeader ? SECTION_HEADER_VIEW : 0; }
它是通过SectionEntity
的isHeader
属性来区别是否是头部的
public abstract class SectionEntity<T> { public boolean isHeader; public T t; public String header; public SectionEntity(boolean isHeader, String header) { this.isHeader = isHeader; this.header = header; this.t = null; } public SectionEntity(T t) { this.isHeader = false; this.header = null; this.t = t; }}
这就是为什么要求开发者的实体类必须继承SectionEntity
的原因了,因为需要通过它的isHeader
这个属性来改变type,onCreateViewHolder
通过不同的type来加载不同的布局。
@Overrideprotected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) { if (viewType == SECTION_HEADER_VIEW) return new BaseViewHolder(getItemView(mSectionHeadResId, parent)); return super.onCreateDefViewHolder(parent, viewType);}
然后在onBindViewHolder
里面通过type来区分头部和内容部分调用不同的方法
protected void convert(BaseViewHolder holder, Object item) { switch (holder.getItemViewType()) { case SECTION_HEADER_VIEW: setFullSpan(holder); convertHead(holder, (T) item); break; default: convert(holder, (T) item); break; }}protected abstract void convertHead(BaseViewHolder helper, T item);protected abstract void convert(BaseViewHolder helper, T item);
setFullSpan
是填充一行的方法,因为要考虑到StaggeredGridLayoutManager的情况。
protected void setFullSpan(RecyclerView.ViewHolder holder) { if (holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) { StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams(); params.setFullSpan(true); } }
以下代码是为了适配GridLayoutManager
@Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); if (manager instanceof GridLayoutManager) { final GridLayoutManager gridManager = ((GridLayoutManager) manager); gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { int type = getItemViewType(position); return (type == EMPTY_VIEW || type == HEADER_VIEW || type == FOOTER_VIEW || type == LOADING_VIEW) ? gridManager.getSpanCount() : 1; }); } }
如果还什么疑问都可以在这里进行提问 因为开源项目和技术分享收到 Google 的面试邀请,大家有什么想要讨论的么?
1 1
- BRVAH分组功能原理分析
- Android 使用BRVAH 实现二级菜单。类似QQ分组
- Tapestry 3.0 输出功能的原理分析
- 交换机与路由器功能原理简单分析
- [转]分析《CityVille》功能设置原理
- DirectPlay分组功能
- spread分组功能设置
- BRVAH官方使用指南
- BRVAH官方使用指南
- RecyclerView分组之BaseRecyclerViewAdapterHelper(实现分组功能)
- 分组密码算法分析
- 聚合分析与分组
- 分组密码算法分析
- 聚合分析和分组
- 分组密码算法分析
- 分组分析制作流程
- 分组分析DEMO
- 关于jqGrid中的分组功能
- 一个项目搞明白Android 启动模式和taskAffinity
- java.lang.IllegalStateException: beginBroadcast() called while already in a broadcast
- 设定闹钟的方法
- Unique Paths II
- 程序猿练级
- BRVAH分组功能原理分析
- CSS 的优先级机制
- 自执行匿名函数
- 计算机中的位bit字节Byte帧frame关系
- Android CalendarView 控件的 getDate() 方法有错误
- 模拟实现strlen
- 获取Activity启动页面的名字
- kernel/sysctl.c
- Impala的中文翻译链接