参考文章:【Android 仿微信通讯录 导航分组列表-上】使用ItemDecoration为RecyclerView打造带悬停头部的分组列表.
Demo:github传送门
使用要点
1.定义列表:
private List<YearElectricResponse.DeviceElectrics> deviceElectricsReturn = new ArrayList<>();
2.recyclerView设置和decoration(自己写Adapter)
adapter = new ...;
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
adapter.bindToRecyclerView(recyclerView);
decoration = new SuspensionDecoration(getContext(),deviceElectricsReturn).setColorTitleFont(R.color.front_gray)
.setTitleLeftPadding(18)
.setmTitleHeight(35);
recyclerView.addItemDecoration(decoration);
3.添加接口
public static class DeviceElectrics implements ISuspensionInterface {
private float value;
private int month;
private int year;
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
//重点是下面这两个
@Override
public boolean isShowSuspension() {
return true;
}
@Override
public String getSuspensionTag() {
String show = String.valueOf(year);
return show;
}
}