最近在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;
    }

它是通过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来加载不同的布局。

@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来区分头部和内容部分调用不同的方法

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的情况。

如果还什么疑问都可以在这里进行提问 因为开源项目和技术分享收到 Google 的面试邀请,大家有什么想要讨论的么?


陈宇明
321 声望114 粉丝

引用和评论

0 条评论