# SectionRecyclerViewDemo
利用sectionedRecyclerViewAdapter实现分组列表的recyclerView
# 示例图片
![image](https://github.com/nbwzlyd/SectionRecyclerViewDemo/blob/master/app/src/main/res/raw/123.gif)
博客链接 https://blog.csdn.net/wzlyd1/article/details/52292548
## 前言 ##
**转载请注明出处**
http://blog.csdn.net/wzlyd1/article/details/52292548
一直在鸿洋大神的安卓群里水群,渐渐的loader和安卓弟等人都成长了起来,还记得当初他们清纯的模样。小L在群里不水了,安卓弟成长为CTO了,只有我依然默默无闻,于是决定再写博客了,之前不写,一是因为工作比较忙,二是因为我水平有限,简单的不想写,因为写了也没用,网上demo很多,难的自己也没多高的造诣,写也写不出来,所以一直都是处于“半荒废状态”,当然说到底其实还是因为懒,于是今天我再次执笔,将学到的东西全部记录下来。
## 效果 ##
先上效果图给大家看看,好有一个整体的认识
![这里写图片描述](http://img.blog.csdn.net/20160823170104563)
效果就是这样的,但是不仅仅局限于这种布局,事实上只要是三段式布局,都可以通过该demo的学习来实现,什么是三段式布局呢,就是有header -content-footer类型的布局,画一个图来解释
![这里写图片描述](http://img.blog.csdn.net/20160823171610462)
比如下面这个图就可以
![这里写图片描述](http://img.blog.csdn.net/20160823172440278)
可以看到,用途还是很广泛的,所以很需要我们去学习一下
## 怎么去实现 ##
gitbub上有一个很牛逼的类,但是貌似知道的人很少,名字叫做SectionedRecyclerViewAdapter ,地址https://github.com/truizlop/SectionedRecyclerView 但是今天我们不去研究她是怎么实现的,我们来研究他怎么用就行了
1. 继承SectionedRecyclerViewAdapter
```
/**
* Created by lyd10892 on 2016/8/23.
*/
public class HotelEntityAdapter extends SectionedRecyclerViewAdapter<HeaderHolder, DescHolder, RecyclerView.ViewHolder> {
public ArrayList<HotelEntity.TagsEntity> allTagList;
private Context mContext;
private LayoutInflater mInflater;
private SparseBooleanArray mBooleanMap;//记录下哪个section是被打开的
public HotelEntityAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(context);
mBooleanMap = new SparseBooleanArray();
}
public void setData(ArrayList<HotelEntity.TagsEntity> allTagList) {
this.allTagList = allTagList;
notifyDataSetChanged();
}
@Override
protected int getSectionCount() {
return HotelUtils.isEmpty(allTagList) ? 0 : allTagList.size();
}
@Override
protected int getItemCountForSection(int section) {
int count = allTagList.get(section).tagInfoList.size();
if (count >= 8 && !mBooleanMap.get(section)) {
count = 8;
}
return HotelUtils.isEmpty(allTagList.get(section).tagInfoList) ? 0 : count;
}
//是否有footer布局
@Override
protected boolean hasFooterInSection(int section) {
return false;
}
@Override
protected HeaderHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
return new HeaderHolder(mInflater.inflate(R.layout.hotel_title_item, parent, false));
}
@Override
protected RecyclerView.ViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
protected DescHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
return new DescHolder(mInflater.inflate(R.layout.hotel_desc_item, parent, false));
}
@Override
protected void onBindSectionHeaderViewHolder(final HeaderHolder holder, final int section) {
holder.openView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isOpen = mBooleanMap.get(section);
String text = isOpen ? "展开" : "关闭";
mBooleanMap.put(section, !isOpen);
holder.openView.setText(text);
notifyDataSetChanged();
}
});
holder.titleView.setText(allTagList.get(section).tagsName);
holder.openView.setText(mBooleanMap.get(section) ? "关闭" : "展开");
}
@Override
protected void onBindSectionFooterViewHolder(RecyclerView.ViewHolder holder, int section) {
}
@Override
protected void onBindItemViewHolder(DescHolder holder, int section, int position) {
holder.descView.setText(allTagList.get(section).tagInfoList.get(position).tagName);
}
}
```
这里面有几个很重要的方法也是需要我们必须重写的,是我们实现效果的关键
```
protected abstract int getSectionCount();
protected abstract int getItemCountForSection(int section);
protected abstract boolean hasFooterInSection(int section);
protected abstract H onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType);
protected abstract F onCreateSectionFooterViewHolder(ViewGroup parent, int viewType);
protected abstract VH onCreateItemViewHolder(ViewGroup parent, int viewType);
protected abstract void onBindSectionHeaderViewHolder(H holder, int section);
protected abstract void onBindSectionFooterViewHolder(F holder, int section);
protected abstract void onBindItemViewHolder(VH holder, int section, int position);
```
接下来我们详细分析这几个方法存在的具体意义
不过在说之前我们需要看一下我们的数据结构,这个也很重要
```
public class HotelEntity {
/**
* 要注意这个类的数据结构,很重要,直接决定了我们能不能实现分组展示
*/
public ArrayList<TagsEntity> allTagsList;
public class TagsEntity {
public String tagsName;
public ArrayList<TagInfo> tagInfoList;
public class TagInfo {
public String tagName;
}
}
}
```
**这个方法主要是用来计算我们一共有多少个section需要展示,返回值是我们最外称list的大小,在我们的示例图中,对应的为热门品牌---商业区---热门景点 等,对应的数据是我们的allTagList**
```
protected abstract int getSectionCount();
```
**这个方法是用来展示content内容区域,返回值是我们需要展示多少内容,在本例中,我们超过8条数据只展示8条,点击展开后就会展示全部数据,逻辑就在这里控制。 对应数据结构为tagInfoList**
```
protected abstract int getItemCountForSection(int section);
```
**判断是否需要底部footer布局,在该例中,我们并不需要显示footer,所以默认返回false就可以,如果你对应的section需要展示footer布局,那么就在对应的section返回true就行了**
```
protected abstract boolean hasFooterInSection(int section);
```
我们要单独说一下这个方法,这里有一个section和position ,有些人可能会弄混
section是区域,也就是我们最外层的index,position是每个section对应的内容数据的position
```
@Override
protected void onBindItemViewHolder(DescHolder holder, int section, int position) {
holder.descView.setText(allTagList.get(section).tagInfoList.get(position).tagName);
}
```
至于下面的onCreateViewHolder ,onBindViewHolder不多做解释了,如果你用过recyclerView,使用方法是一样的,无非是渲染布局,绑定数据
- **展示数据**
基本上,如果上面的adapter逻辑写完,我们的布局算是完成了,首页代码如下
```
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private HotelEntityAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
没有合适的资源?快使用搜索试试~ 我知道了~
sectionedRecyclerViewAdapter实现分组列表的RecyclerView
共1468个文件
flat:721个
xml:251个
json:229个
3星 · 超过75%的资源 需积分: 10 14 下载量 66 浏览量
2018-10-18
15:36:31
上传
评论 1
收藏 13.51MB RAR 举报
温馨提示
利用sectionedRecyclerViewAdapter实现分组列表的recyclerView 每组可以展示隐藏
资源推荐
资源详情
资源评论
收起资源包目录
sectionedRecyclerViewAdapter实现分组列表的RecyclerView (1468个子文件)
+6BaVZ4aTVKpFu0hVrrRw3VTTt8= 588KB
+6BaVZ4aTVKpFu0hVrrRw3VTTt8= 588KB
8UaJ063TQitHir46klm_rRUNfm8= 19KB
8UaJ063TQitHir46klm_rRUNfm8= 19KB
resources-debugAndroidTest.ap_ 342KB
resources-debug.ap_ 294KB
app-debug.apk 1.34MB
gradle-wrapper.properties.bak 231B
gradlew.bat 2KB
fileSnapshots.bin 873KB
classAnalysis.bin 810KB
jarAnalysis.bin 362KB
fileHashes.bin 158KB
taskHistory.bin 65KB
taskHistory.bin 43KB
taskJars.bin 20KB
resourceHashesCache.bin 19KB
last-build.bin 1B
last-build.bin 1B
built.bin 0B
R$styleable.class 23KB
R$styleable.class 20KB
R$style.class 20KB
R$style.class 20KB
R$attr.class 10KB
R$attr.class 9KB
SectionedRecyclerViewAdapter.class 6KB
R$dimen.class 5KB
HotelEntityAdapter.class 5KB
R$dimen.class 5KB
R$drawable.class 4KB
R$drawable.class 4KB
R$color.class 4KB
R$color.class 4KB
R$id.class 4KB
R$id.class 4KB
R$styleable.class 3KB
R$layout.class 2KB
MainActivity.class 2KB
R$layout.class 2KB
R$attr.class 2KB
FileUtils.class 2KB
R$string.class 2KB
HotelEntityAdapter$1.class 2KB
R$string.class 1KB
SectionedSpanSizeLookup.class 1KB
R.class 1KB
R.class 1015B
SectionedRecyclerViewAdapter$SectionDataObserver.class 940B
HeaderHolder.class 910B
R$bool.class 896B
JsonUtils.class 878B
R$bool.class 872B
DescHolder.class 849B
R$anim.class 837B
R$anim.class 813B
R$styleable.class 809B
BuildConfig.class 765B
HotelEntity$TagsEntity.class 685B
R$integer.class 681B
R$integer.class 657B
HotelUtils.class 600B
R$dimen.class 599B
HotelEntity$TagsEntity$TagInfo.class 583B
R.class 559B
R$attr.class 542B
R$mipmap.class 483B
HotelEntity.class 474B
R.class 473B
R$id.class 453B
R$raw.class 439B
R$id.class 408B
R.class 314B
R.class 296B
R.class 266B
cN7ImG5L+3t9S57lofC96Omadbs= 1.02MB
cN7ImG5L+3t9S57lofC96Omadbs= 1.02MB
cXdY3MKO9EA6VURpI9G3rDkgZbM= 291KB
cXdY3MKO9EA6VURpI9G3rDkgZbM= 291KB
classes.dex 2.95MB
classes.dex 2.19MB
R$styleable.dex 20KB
R$style.dex 19KB
R$style.dex 19KB
R$styleable.dex 18KB
R$attr.dex 10KB
R$attr.dex 9KB
R$dimen.dex 5KB
R$dimen.dex 5KB
R$drawable.dex 5KB
SectionedRecyclerViewAdapter.dex 5KB
R$drawable.dex 5KB
R$color.dex 4KB
R$color.dex 4KB
R$id.dex 4KB
HotelEntityAdapter.dex 4KB
R$id.dex 4KB
R$styleable.dex 3KB
R$layout.dex 3KB
R$layout.dex 3KB
共 1468 条
- 1
- 2
- 3
- 4
- 5
- 6
- 15
资源评论
- 书船雨眠2019-05-05可以参考一下的
徐玮大人
- 粉丝: 191
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功