最近在diycode社區遇到一位同學提問,所以特寫此文章來分析BRVAH分組功能的實現。如果還什么疑問都可以在這里進行提問 因為開源項目和技術分享收到 Google 的面試邀請,大家有什么想要討論的么?
問題分析的步驟:
- 如何使用
- 原理分析
如何該框架的分組功能
[圖片上傳失敗...(image-b9b04b-1533306354483)]
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
方法的原因了。