timehop / sticky-headers-recyclerview

[UNMAINTAINED] Sticky Headers decorator for Android's RecyclerView
Apache License 2.0
3.74k stars 756 forks source link

Header click returns 0 postion pleasee help me #117

Open khajievN opened 8 years ago

khajievN commented 8 years ago

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 !!!!

AX-NICOLAS commented 8 years ago

not response ? same bug...

khajievN commented 8 years ago

Please can someone solve this problem ??

qinweiforandroid commented 8 years ago

我也遇到了 这个问题 顶部的header callback 返回的postion 一直没有变 always 0

khajievN commented 8 years ago

what i didn't understand any words please write in English

jacobtabak commented 8 years ago

This library is no longer under active maintenance as is noted in README.MD

AlbertVilaCalvo commented 8 years ago

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
}
qinweiforandroid commented 8 years ago

Albertvilacalvo thank you very much , The bug is solved.

Ranjith121 commented 8 years ago

Same issue arise even changing this code. pls help

ZhangFengG commented 8 years ago

@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

AlbertVilaCalvo commented 8 years ago

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.

jianhui1012 commented 5 years ago

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);
            }
        }
    }
sorrybeman commented 2 years ago

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);
            }
        }
    }

This is the anwser

mHeaderRects.clear();