前言:
在ExpandableRecyclerView的基础上实现的StickyRecyclerView,支持水平与垂直方向, 支持GridLayoutManager和StaggeredGridLayoutManager布局和自定义LayoutManager扩展, 支持StickyHeader和下一个StickyHeader的渐变背景文字颜色动画,支持画的方式与Gravity和任意布局方式,Header,Footer, 支持StickyManager扩展
先看功能图和效果图(查看原图动画)
一: 使用, 没有过度的封装
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.youxiaochen:sticky-recyclerview:1.0.4'
}
private var stickyView: StickyRecyclerView...
private var adapter: StickAdapter...
private var indexView: CharIndexView...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......
stickyView.setStickyAdapter(adapter)
CharIndexMediator(stickyView, indexView).attach(this) /** 交给*/
}
亦可使用代码生成控件
//用于代码生成控件
public StickyRecyclerView(@NonNull Context context, @NonNull StickyParams params) {
this(context, params, new DrawingStickyManager());
}
//用于代码生成控件
public StickyRecyclerView(@NonNull Context context, @NonNull StickyParams params, @NonNull StickyManager stickyManager) {
super(context);
initialize(params, stickyManager);
}
二: 相关类
1. CharIndexView相关属性, 配合CharIndexMediator使用更方便
内部Adapter类
public static abstract class Adapter {
......
//字符索引数量
public abstract int getItemCount();
//当前字符索引
public abstract char getItemChar(int position);
}
CharIndexView相关属性 |
说明 |
indexOrientation |
布局方向支持垂直vertical/水平方向horizontal |
indexItemSize |
画单个字符的item正方形矩阵大小 |
indexTextSize |
索引字符的大小 |
indexTextColor, selectIndexTextColor |
索引字符颜色和滑动select时当前颜色 |
selectIndexBgDrawable |
索引字符select的背景drawable |
indexDrawableStart |
类似TextView的水平时drawableLeft/垂直时drawableTop |
indexDrawablePadding |
类似类似TextView的drawablePadding |
indicatorTextSize, indicatorTextColor |
touch时的指示器文字大小 |
indicatorBgDrawable, indicatorPadding |
指示器背景(支持.9)及与索引字符的padding距离 |
indicatorAutoCheck |
自动校正与Sticky的当前index一致 |
2. StickyRecyclerView相关属性 extends ExpandableRecyclerView
内部StickyAdapter类, 用法与ExpandableAdapter一致
public static abstract class StickyAdapter<GVH extends GroupViewHolder, CVH extends ChildViewHolder> extends ExpandableAdapter<GVH, CVH> {
@Override
public boolean groupCanClick() {
return false;
}
@Override
public void onGroupViewAttachedToWindow(@NonNull GVH holder) {
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
if ((params instanceof StaggeredGridLayoutManager.LayoutParams)) {
StaggeredGridLayoutManager.LayoutParams sParams = (StaggeredGridLayoutManager.LayoutParams) params;
sParams.setFullSpan(true);
}
}
//获取Sticky的item对象
@NonNull
public abstract String getStickerItem(int groupPos);
}
StickyRecyclerView相关属性 |
说明 |
stickySize |
Sticky的Header大小垂直时为高度, 水平时为宽度 |
stickyTextSize |
Sticky画的Header字符大小 |
stickyTextColor |
字符颜色,支持selctor,配置state_first=true时即为Stickied颜色见demo |
stickyBackgroundColor |
Sticky背景颜色支持selector 配置state_first=true时即为Stickied颜色见demo |
stickyGravity |
字符的Gravity,支持start,center,end, 可配合stickyMargin |
stickyMargin |
配合stickyGravity,相当marginStart, marginEnd, center时(width/2 + margin/2) |
StickyManager支持重写扩展 setStickyManager(stickyManager...)
3. 适用任意布局时的StickyLayoutView相关属性 extends FrameLayout
内部Adapter类, 用法与ExpandableAdapter一致
public static abstract class Adapter<GVH extends GroupViewHolder, CVH extends ChildViewHolder> extends ExpandableAdapter<GVH, CVH> {
/**
* 创建StickyViewHolder, 不与ExpandableRecyclerView中的GroupViewHolder混合复用
* 若要监听点击事件亦可在此方法中设置
*/
public abstract @NonNull GVH onCreateStickyViewHolder(@NonNull ViewGroup parent);
//绑定当前StickyViewHolder
public abstract void onBindStickyViewHolder(@NonNull GVH holder, int groupPos);
@Override
public boolean groupCanClick() {
return false;
}
}