Open khajievN opened 8 years ago
not response ? same bug...
Please can someone solve this problem ??
我也遇到了 这个问题 顶部的header callback 返回的postion 一直没有变 always 0
what i didn't understand any words please write in English
This library is no longer under active maintenance as is noted in README.MD
I've solved the problem that the position is wrong (0 or other) if the header is at the top.
I've fixed this by downloading the files and copy-pasting them to my project. Then I modifyed StickyRecyclerHeadersDecoration findHeaderPositionUnder()
. The original (wrong) code is:
public int findHeaderPositionUnder(int x, int y) {
for (int i = 0; i < mHeaderRects.size(); i++) {
Rect rect = mHeaderRects.get(mHeaderRects.keyAt(i));
if (rect.contains(x, y)) {
int position = mHeaderRects.keyAt(i);
if (mVisibilityAdapter == null || mVisibilityAdapter.isPositionVisible(position)) {
return position;
}
}
}
return -1;
}
You should change it like this:
public int findHeaderPositionUnder(int x, int y) {
int tempPosition = -1; // Added
for (int i = 0; i < mHeaderRects.size(); i++) {
Rect rect = mHeaderRects.get(mHeaderRects.keyAt(i));
if (rect.contains(x, y)) {
int position = mHeaderRects.keyAt(i);
if (mVisibilityAdapter == null || mVisibilityAdapter.isPositionVisible(position)) {
tempPosition = position; // Added
}
}
}
return tempPosition; // Added
}
Albertvilacalvo thank you very much , The bug is solved.
Same issue arise even changing this code. pls help
@AlbertVilaCalvo your modifyed code always return a wrong position.see this change:
public int findHeaderPositionUnder(int x, int y) {
int tempPosition = -1; // Added
for (int i = 0; i < mHeaderRects.size(); i++) {
Rect rect = mHeaderRects.get(mHeaderRects.keyAt(i));
if (rect.contains(x, y)) {
int position = mHeaderRects.keyAt(i);
if (mVisibilityAdapter == null || mVisibilityAdapter.isPositionVisible(position)) {
tempPosition = position; // Added
}else {
if (tempPosition!=-1) {
break;//my Added
}
}
}
}
return tempPosition; // Added
}
@Ranjith121 try this
Thanks @ZhangFengG I haven't had any issues with my code but other people might need your changes.
Maybe I should say that in my RecyclerView every single row has a sticky header.
The code above has problems when show items in group, I solved it with the code below.
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(canvas, parent, state);
final int childCount = parent.getChildCount();
if (childCount <= 0 || mAdapter.getItemCount() <= 0) {
return;
}
// my Added
mHeaderRects.clear();
for (int i = 0; i < childCount; i++) {
View itemView = parent.getChildAt(i);
int position = parent.getChildAdapterPosition(itemView);
if (position == RecyclerView.NO_POSITION) {
continue;
}
boolean hasStickyHeader = mHeaderPositionCalculator.hasStickyHeader(itemView, mOrientationProvider.getOrientation(parent), position);
if (hasStickyHeader || mHeaderPositionCalculator.hasNewHeader(position, mOrientationProvider.isReverseLayout(parent))) {
View header = mHeaderProvider.getHeader(parent, position);
boolean areHeadersSticky = true;
if (header.getTag() != null) {
///areHeadersSticky = !(boolean) header.getTag();
}
//re-use existing Rect, if any.
Rect headerOffset = mHeaderRects.get(position);
if (headerOffset == null) {
headerOffset = new Rect();
mHeaderRects.put(position, headerOffset);
}
mHeaderPositionCalculator.initHeaderBounds(headerOffset, parent, header, itemView, hasStickyHeader, areHeadersSticky);
mRenderer.drawHeader(parent, canvas, header, headerOffset);
}
}
}
The code above has problems when show items in group, I solved it with the code below.
@Override public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) { super.onDrawOver(canvas, parent, state); final int childCount = parent.getChildCount(); if (childCount <= 0 || mAdapter.getItemCount() <= 0) { return; } // my Added mHeaderRects.clear(); for (int i = 0; i < childCount; i++) { View itemView = parent.getChildAt(i); int position = parent.getChildAdapterPosition(itemView); if (position == RecyclerView.NO_POSITION) { continue; } boolean hasStickyHeader = mHeaderPositionCalculator.hasStickyHeader(itemView, mOrientationProvider.getOrientation(parent), position); if (hasStickyHeader || mHeaderPositionCalculator.hasNewHeader(position, mOrientationProvider.isReverseLayout(parent))) { View header = mHeaderProvider.getHeader(parent, position); boolean areHeadersSticky = true; if (header.getTag() != null) { ///areHeadersSticky = !(boolean) header.getTag(); } //re-use existing Rect, if any. Rect headerOffset = mHeaderRects.get(position); if (headerOffset == null) { headerOffset = new Rect(); mHeaderRects.put(position, headerOffset); } mHeaderPositionCalculator.initHeaderBounds(headerOffset, parent, header, itemView, hasStickyHeader, areHeadersSticky); mRenderer.drawHeader(parent, canvas, header, headerOffset); } } }
mHeaderRects.clear();
In the first poation header click returns 0 it is correct but when this sticky header overlay the view it returns 0 all the time. Please help me it is really important !!!!