emilsjolander / StickyListHeaders

An android library for section headers that stick to the top
Apache License 2.0
5.51k stars 1.52k forks source link

Can we smoothScroll to a particular header? #284

Closed iamsachin closed 10 years ago

iamsachin commented 10 years ago

I am trying to smooth scroll within various groups of headers having items within them. Is there any method for scrolling to a particular header?

emilsjolander commented 10 years ago

Just call stickyList.smoothScrollToPosition(headerPosition) where headerPostion is the position of the header you want to scroll to in the list.

iamsachin commented 10 years ago

Hi. Thank you for the quick response. I tried using this, but this is taking me to the child elements of the header with position relative to the first child element. It's not moving to the headers themselves. I am trying to do this: In my taxi booking app, I have a history page, where there are 4 types of headers, "booked, "confirmed", "cancelled" and "completed". Based on the booking status in the database, the cursor adapter fills the stickyheaders list view. I made 4 buttons at the bottom with the same names. Now I am trying this, when I click on cancelled button, it should take me to the cancelled header with all the cancelled bookings below it. These are few of my methods that I have overridden: ''' @Override public int getCount() { return c.getCount(); }

@Override
public Object getItem(int position) {
    c.moveToPosition(position);
    return c;
}

private int getItemViewType(Cursor cursor) {
    String type = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(10)));
    if (type.equals("Booked")) {
        return 0;
    } else if (type.equals("Confirmed")) {
        return 1;
    } else if (type.equals("Completed")){
        return 2;
    } else {
        return 3;
    }
}

@Override
public int getItemViewType(int position) {
    c.moveToPosition(position);
    return getItemViewType(c);
}

@Override
public int getViewTypeCount() {
    return 4;
}

@Override
public long getHeaderId(int i) {
    c.moveToPosition(i);
    if (c.getString(c.getColumnIndex(c.getColumnName(10))).equals("Confirmed")) {
        return 2;
    } else if (c.getString(c.getColumnIndex(c.getColumnName(10))).equals("Booked")) {
        return 1;
    } else if (c.getString(c.getColumnIndex(c.getColumnName(10))).equals("Completed")) {
        return 3;
    } else{
        return 4;
    }
}

'''

emilsjolander commented 10 years ago

I might be understanding you wrong but if you have a list that looks like this

Header 1
- item
- item
- item
Header 2
- item
- item
Header 3
- item
- item
- item
- item

and you want to move to Header 3 you just call stickyList.smoothScrollToPosition(5) because you want to get to the 5th item in the list (the first item under Header 3)

It would be nice to add a method like smoothScrollToHeader(int header) though. A downside with this is that SLH does not know the positions of headers and would need to travers the whole dataset to find headers. This should however not be a problem for most lists.