turing-tech / MaterialScrollBar

An Android library that brings the Material Design 5.1 sidebar to pre-5.1 devices.
Apache License 2.0
778 stars 126 forks source link

RecyclerView with different size rows #56

Closed Marcelhozeman closed 8 years ago

Marcelhozeman commented 8 years ago

Hello,

I've got a RecyclerView with multiple ViewHolders that all have a separate layout file with a different height (for example 20dp, 40dp and 60dp). So each row has a different (but static) height. When i use the TouchScrollBar it doesn't scroll smoothly trough the items. Is there a way to support this?

turing-tech commented 8 years ago

Have your adapter extend ICustomScrolling and invoke the useCustomScolling() method on your bar setup.

Marcelhozeman commented 8 years ago

Thanks! I forgot to set useCustomScrolling. Didn't see that method in the header tutorial. I've implemented this 2 interfaces now: FastScrollerUtil.IHeaderAdapter, ICustomScroller. And i am returning the height of my biggest ViewHolder in the getRowHeight method (IHeaderAdapter interface method). Is this the correct way to do this?

MFlisar commented 8 years ago

This won't work correctly. The logic behind the HeaderAdapter is 1 header and 1 item, whereas every header has the same height and every item has the same height...

turing-tech commented 8 years ago

Exactly. You'll need to remove that interface (keep ICustomScrolling) and fill in the methods yourself.

Marcelhozeman commented 8 years ago

Thanks guys,

It works now.

Mina-H-Samy commented 8 years ago

Just wanted to say thanks very much for this great library! The only library I could find where I could have different row heights with scrolling working as expected.

joaquincorrales commented 7 years ago

@MinaHany @Marcelhozeman Hey, can you show some adapter code as far as implementing the IHeaderAdapter and ICustomScroller? We followed the Header tutorial to the tee but have issues with scrolling. It only seems to get one letter and doesn't work anywhere near expected. We're sure we did something wrong but have spent hours and are unable to figure it out. Appreciate it!

turing-tech commented 7 years ago

If you could provide a sample of your code that would be the easiest. If not I can write up an example tomorrow.

Mina-H-Samy commented 7 years ago

@joaquincorrales I am not implementing IHeaderAdapter, just ICustomScroller so not sure if my example will help but here it is anyway:

private List<Integer> sectionHeights;

@Override
public int getDepthForItem(int index) {
    return getDepth(index);
}

@Override
public int getItemIndexForScroll(float progress) {
    float depth = 0;
    float totalDepth = getTotalDepth();
    for (int i = 0; i < sectionHeights.size(); i++) {
        depth += sectionHeights.get(i);
        if (progress <= depth / totalDepth) {
            return i;
        }
    }
    return sectionHeights.size() - 1;
}

@Override
public int getTotalDepth() {
    return getDepth(sectionHeights.size());
}

private int getDepth(int endIndex) {
    int depth = 0;
    for (int i = 0; i < endIndex; i++) {
        depth += sectionHeights.get(i);
    }
    return depth;
}