简体   繁体   中英

Android: StickyGridHeaders: setSelection to an item

I'm using the StickyGridHeader library for a gridview. I want to scroll the gridview to a specific location based on the item that it should be displaying in a view.

I've tried using setSelection to the index of that item in the gridview that I expect to use, but it has a tendency to overshoot or undershoot the location that I expect in grids with different numbers of columns.

I am able to set the correct location in a grid with one column (essentially a list) using setSelection and finding the item this way:

    event = getEventThatWeWantByDate();
    //Find and store event's position in the gridview
    for( int i = 0; i < getGridView().getAdapter().getCount(); i++ ) {
        gridEvent = (Event) getGridView().getItemAtPosition( i );
        if( !( gridEvent instanceof Event ) )
            continue;
        if( gridEvent.getEventDate() == event.getEventDate() ) {
            index = i;
            break;
        }
    }

Then I use index to bring it up. I also have another function for counting the amount of blanks in my grid:

    int blanks = 0;
    int sumOfAllLastRowItems = 0;
    //Get the number of blanks in the grid up until the desired item
    for( int i = 0; i < headers; i++ ) {
        int numOfItemsInLastRow = wrapper.getCountForHeader( i ) % getResources().getInteger( R.integer.event_grid_num_columns );
        //Log.e( TAG, "Num in area: " + numOfItemsInLastRow );
        if( numOfItemsInLastRow != 0 ) {
            sumOfAllLastRowItems += numOfItemsInLastRow;
            int numOfBlankSpaces = ( getResources().getInteger( R.integer.event_grid_num_columns ) ) - numOfItemsInLastRow;
        //  Log.e( TAG, "Adding num of blanks: " + numOfBlankSpaces );
            blanks += numOfBlankSpaces;
        }
        else sumOfAllLastRowItems += getResources().getInteger( R.integer.event_grid_num_columns );
    }

As well as the number of headers before the item that I want:

    StickyGridHeadersSimpleAdapterWrapper wrapper = new StickyGridHeadersSimpleAdapterWrapper( (StickyEventViewGridAdapter) getAdapter() );
    int headers = 0;
    //Count number of headers before the requested event
    for( int i = 0, j = 0; i < wrapper.getNumHeaders() ; i++ ) {
        j += wrapper.getCountForHeader( i );
        headers++;
        if( j >= arrayIndex ) {
            break;
        }
    }

I've tried adding the amount of blanks and headers to the index, and that works great if there's two columns, but then if I make the grid use 3 columns it will go an unrelated location again.

Any suggestions?

02-06 16:31:03.147 8711-8711/? E/EventGridFragment﹕ Number of Headers Before Item: 24

02-06 16:31:03.147 8711-8711/? E/EventGridFragment﹕ Sticky Grid Adapter Index: 71

02-06 16:31:03.147 8711-8711/? E/EventGridFragment﹕ Array Adapter Index: 45

02-06 16:31:03.147 8711-8711/? E/EventGridFragment﹕ Blanks: 28

02-06 16:31:03.147 8711-8711/? E/EventGridFragment﹕ Value to return: 123

I'm actually looking for an item ~150 in the gridview when my number of columns is 3.

StickyGridHeader fills in each row, including the header, with empty views to fill them up to the numOfColumns. This means that if you have a grid with 5 columns, each header will consist of 5 views, as well as each empty space counting as a view. I ended up having to count the number of rows until the item I wanted to display and do this:

getGridView.setSelection( ( numOfHeaders + numOfRows ) * getResources().getInteger( R.integer.event_grid_num_columns ) );

When you're finding the index in the first code block of your question, you're not wrapping the adapter so you count all the header entries and all the blank cells it's used to make the layout look nice:

for( int i = 0; i < getGridView().getAdapter().getCount(); i++ ) {
    gridEvent = (Event) getGridView().getItemAtPosition( i )

The convenience method on the second line gets you the item from a normal grid view, not a StickyGridHeaders grid view. Instead, use:

StickyEventViewGridAdapter adapter = (StickyEventViewGridAdapter) getGridView().getAdapter();

for( int i = 0; i < adapter.getCount(); i++ ) {
    gridEvent = (Event) adapter.getItem(i)

By wrapping the adapter you should avoid this without having to do your other code blocks to count blanks and header cells. The position you would get back would be the one you can refer to in setSelection()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM