[英]Recycler ItemDecoration item appearing over recycler view items
I needed to add sticky header in recyclerview. 我需要在recyclerview中添加粘性标头。 I used
RosterRecyclerItemDecoration
and it gave me the desired result. 我使用了
RosterRecyclerItemDecoration
,它给了我想要的结果。
The issue is that the Decorator
item is appearing over the items of recycler view
. 问题是
Decorator
项目出现在recycler view
的项目上方。
I want to add decorator item in between of recyclerview items and not over recyclerview items. 我想在recyclerview项目之间添加装饰器项目,而不是在recyclerview项目之间添加装饰器项目。
This is my code. 这是我的代码。
RosterRecyclerItemDecoration rosterRecyclerItemDecoration =
new RosterRecyclerItemDecoration(true,
this);
rvLeaders.addItemDecoration(rosterRecyclerItemDecoration);
Decorator Class: 装饰类:
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;
}
}
}
}
You have to use Rect to draw your decorator. 您必须使用Rect绘制装饰器。 In your
RosterRecyclerItemDecoration
Constructor you can pass a height like 40 or 50 dp it is dpend on your Layout. 在您的
RosterRecyclerItemDecoration
构造函数中,您可以传递40或50 dp的高度,它是在Layout上的高度。
Now In your RosterRecyclerItemDecoration
class override getItemOffsets
It looks like: 现在在您的
RosterRecyclerItemDecoration
类中重写getItemOffsets
它看起来像:
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
}
in this override method you can set your height. 在此替代方法中,您可以设置高度。 Like
outRect.top = headerOffset;
就像
outRect.top = headerOffset;
[Check this view is your sectionHeader] [检查此视图是否为您的sectionHeader]
Note: Here headerOffset is height that you send in your RosterRecyclerItemDecoration
constructor. 注意:此处headerOffset是您在
RosterRecyclerItemDecoration
构造函数中发送的高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.