smoothScrollToPositionFromTop() is not always working like it should

前端 未结 5 635
失恋的感觉
失恋的感觉 2020-11-27 15:26

I\'ve been trying for a while to get smoothScrollToPositionFromTop() working, but it doesn\'t always scroll to the correct position.

I\'ve got a ListView (with 10 i

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 16:13

    Here is an implementation of the solution.

        void smoothScrollToPositionFromTopWithBugWorkAround(final AbsListView listView,
                                                        final int position,
                                                        final int offset, 
                                                        final int duration){
    
        //the bug workaround involves listening to when it has finished scrolling, and then 
        //firing a new scroll to the same position.
    
        //the bug is the case that sometimes smooth Scroll To Position sort of misses its intended position. 
        //more info here : https://code.google.com/p/android/issues/detail?id=36062
        listView.smoothScrollToPositionFromTop(position, offset, duration);
        listView.setOnScrollListener(new OnScrollListener() {
    
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState==OnScrollListener.SCROLL_STATE_IDLE){
                    listView.setOnScrollListener(null);
                    listView.smoothScrollToPositionFromTop(position, offset, duration);
                }
    
            }
    
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });
    }
    

提交回复
热议问题