最近在diycode社区遇到一位同学提问,所以特写此文章来分析BRVAH分组功能的实现。如果还什么疑问都可以在这里进行提问 因为开源项目和技术分享收到 Google 的面试邀请,大家有什么想要讨论的么?
问题分析的步骤:
- 如何使用
- 原理分析
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来加载不同的布局。
@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
方法是干嘛的呀?
因为要考虑到RecyclerView的setLayoutManager
的三种不同类型的LayoutManager
- LinearLayoutManager
- GridLayoutManager
- StaggeredGridLayoutManager
LinearLayoutManager:本身就可以占据一行,所以不需要做特殊的处理
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 == SECTION_HEADER_VIEW) ? gridManager.getSpanCount() : 1;
});
}
}
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);
}
}
这就是为什么要在conver
方法里判断是head类型的代码块中要调用setFullSpan
方法的原因了。
如果还什么疑问都可以在这里进行提问 因为开源项目和技术分享收到 Google 的面试邀请,大家有什么想要讨论的么?