Wait unitl ListView's smoothScrollToPosition() finishes

前端 未结 2 1701
灰色年华
灰色年华 2020-12-28 18:15

Scope

I need to scroll to certain position smoothly and then "jump" to another position with setSelection(anotherPosition). This is done to c

2条回答
  •  攒了一身酷
    2020-12-28 18:54

    Another way is to add an OnScrollListener.

    private final int scrollableItems = 20;
    
    int firstVisiblePosition = mListView.getFirstVisiblePosition();
    if (firstVisiblePosition < scrollableItems) {
        mListView.smoothScrollToPosition(0);
    } else {
    
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(AbsListView absListView, int i) {
            if (i == SCROLL_STATE_IDLE) {
              mListView.setSelection(0);
               }
          }
       })
    
       mListView.smoothScrollToPosition(firstVisiblePosition - scrollableItems);
    }
    mListView.clearFocus();
    

提交回复
热议问题