我需要在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;
}
}
}
}
答案 0 :(得分:0)
您必须使用 Rect 绘制装饰器。在您的RosterRecyclerItemDecoration
构造函数中,您可以传递40或50 dp的高度,它是dpend在您的布局上的。
现在在您的RosterRecyclerItemDecoration
类中覆盖getItemOffsets
看起来像:
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
}
在此替代方法中,您可以设置高度。像outRect.top = headerOffset;
一样[检查此视图是否为您的sectionHeader]
注意:这里的headerOffset是您在RosterRecyclerItemDecoration
构造函数中发送的高度。