public static int scrollToIndex ( String listRef , int itemIndex ) throws Exception { int listViewIndex = getListViewIndex ( listRef ) ; if ( itemIndex >= numItemsInList ( listRef ) ) throw new Exception ( "Item index is greater than number of items in the list" ) ; // scroll to the top of this view if we already passed the index being looked for QueryBuilder builder = new QueryBuilder ( ) ; int firstVisiblePosition = builder . map ( "solo" , "getCurrentViews" , "android.widget.ListView" ) . call ( "get" , listViewIndex ) . call ( "getFirstVisiblePosition" ) . execute ( ) . getInt ( 0 ) ; if ( firstVisiblePosition > itemIndex ) scrollToTop ( ) ; boolean scrollDown = true ; // need to get the wanted item onto the screen while ( true ) { builder = new QueryBuilder ( ) ; firstVisiblePosition = builder . map ( "solo" , "getCurrentViews" , "android.widget.ListView" ) . call ( "get" , listViewIndex ) . call ( "getFirstVisiblePosition" ) . execute ( ) . getInt ( 0 ) ; builder = new QueryBuilder ( ) ; int headersViewsCount = builder . map ( "solo" , "getCurrentViews" , "android.widget.ListView" ) . call ( "get" , listViewIndex ) . call ( "getHeaderViewsCount" ) . execute ( ) . getInt ( 0 ) ; firstVisiblePosition = firstVisiblePosition - headersViewsCount ; builder = new QueryBuilder ( ) ; int visibleChildCount = builder . map ( "solo" , "getCurrentViews" , "android.widget.ListView" ) . call ( "get" , listViewIndex ) . call ( "getChildCount" ) . execute ( ) . getInt ( 0 ) ; int wantedPosition = itemIndex - firstVisiblePosition ; if ( wantedPosition >= visibleChildCount ) { // check to see if we can even scroll anymore if ( ! scrollDown ) { break ; } scrollDown = Solo . scrollDownList ( listViewIndex ) ; } else { // return the position return wantedPosition ; } } throw new Exception ( "Could not find item" ) ; }
Scrolls the specified item index onto the screen and returns the offset based on the current visible list