我需要在 recyclerview 中添加粘性标题。我用过RosterRecyclerItemDecoration
,它给了我想要的结果。
问题是该Decorator
项目出现在recycler view
.
我想在 recyclerview 项目之间添加装饰器项目,而不是在 recyclerview 项目之间添加。
这是我的代码。
RosterRecyclerItemDecoration rosterRecyclerItemDecoration =
new RosterRecyclerItemDecoration(true,
this);
rvLeaders.addItemDecoration(rosterRecyclerItemDecoration);
装饰类:
public class RosterRecyclerItemDecoration extends RecyclerView.ItemDecoration {
public RosterRecyclerItemDecoration(boolean sticky,@NonNull IDecoraterCallback iDecoraterCallback){
this.sticky= sticky;
this.iDecoraterCallback= iDecoraterCallback;
}
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDrawOver(c, parent, state);
CharSequence previousHeader = "";
if (headerView == null) {
headerView = inflateHeaderView(parent);
header = (TextView) headerView.findViewById(R.id.tvHeader);
fixLayoutSize(headerView, parent);
}
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
final int position = parent.getChildAdapterPosition(child);
if(iDecoraterCallback.getSectionHeader(position) !=null) {
CharSequence title = iDecoraterCallback.getSectionHeader(position);
header.setText(title);
if (!previousHeader.equals(title)) {
drawHeader(c, child, headerView);
previousHeader = title;
}
}
}
}