最近在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 的面試邀請,你們有什麼想要討論的麼?