BRVAH分組功能原理分析

最近在diycode社區遇到一位同窗提問,因此特寫此文章來分析BRVAH分組功能的實現。若是還什麼疑問均可以在這裏進行提問 由於開源項目和技術分享收到 Google 的面試邀請,你們有什麼想要討論的麼?
java

問題分析的步驟:git

  1. 如何使用github

  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源碼ide

@Override
    protected int getDefItemViewType(int position) {
        return ((SectionEntity) mData.get(position)).isHeader ? SECTION_HEADER_VIEW : 0;
    }

它是經過SectionEntityisHeader屬性來區別是不是頭部的佈局

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

相關文章
相關標籤/搜索