我尝试实现Sevastyan Savanyuk所说的粘性标头:
How can I make sticky headers in RecyclerView? (Without external lib)
我在外部回收站视图中有一个嵌套的回收站视图。我只希望我的内部回收站视图具有粘性页眉。
但是onDrawOver
方法仅被调用一次。
结果是我在onDrawOver
中绘制了标题,但它并不粘手!
我只有一个粘性标头(项目0),这是我的代码(kotlin):
override fun isHeader(itemPosition: Int): Boolean = itemPosition == 0
override fun getHeaderPositionForItem(itemPosition: Int): Int = 0
这是我的onDrawOver方法:
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDrawOver(c, parent, state);
View topChild = parent.getChildAt(0);
if (topChild == null) {
return;
}
int topChildPosition = parent.getChildAdapterPosition(topChild);
if (topChildPosition == RecyclerView.NO_POSITION) {
return;
}
int headerPos = mListener.getHeaderPositionForItem(topChildPosition);
View currentHeader = getHeaderViewForItem(headerPos, parent);
fixLayoutSize(parent, currentHeader);
int contactPoint = currentHeader.getBottom();
View childInContact = getChildInContact(parent, contactPoint, headerPos);
if (childInContact != null) {
int itemPosition = parent.getChildAdapterPosition(childInContact);
if (mListener.isHeader(itemPosition)) {
moveHeader(c, currentHeader, childInContact);
return;
}
}
drawHeader(c, currentHeader);
}
我如何使标头保持粘性,为什么onDrawOver
方法仅被调用一次,有帮助!