最近在diycode社区遇到一位同窗提问,因此特写此文章来分析BRVAH分组功能的实现。若是还什么疑问均可以在这里进行提问 由于开源项目和技术分享收到 Google 的面试邀请,你们有什么想要讨论的么?
java
问题分析的步骤:git
如何使用github
原理分析面试
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
以下:框架
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
源码ide
@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来加载不一样的布局。ui
@Override protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) { if (viewType == SECTION_HEADER_VIEW) return new BaseViewHolder(getItemView(mSectionHeadResId, parent)); return super.onCreateDefViewHolder(parent, viewType); }
而后在onBindViewHolder
里面经过type来区分头部和内容部分调用不一样的方法this
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
是填充一行的方法,由于要考虑到多种LayoutManager的状况。spa
若是还什么疑问均可以在这里进行提问 由于开源项目和技术分享收到 Google 的面试邀请,你们有什么想要讨论的么?