等待单元1 ListView的smoothScrollToPosition()完​​成单元、ListView、smoothScrollToPosition

2023-09-12 23:57:01 作者:旧人不归

范围

需要滚动到一定位置平稳地再跳到另一positoin为setSelection(anotherPosition)。这样做是为了创造(如)平滑滚动100个项目中的ListView一种错觉。 smoothScrollToPosition(100)持续时间太多了,你知道的。

问题

setSelection()不会等到smoothScrollToPosition完成它的工作,所以setSelection()被立即调用,用户只能看到快速跳跃;

code

 私人最终诠释scrollableItems = 20;

INT firstVisiblePosition = mListView.getFirstVisiblePosition();
如果(firstVisiblePosition< scrollableItems){
    mListView.smoothScrollToPosition(0);
} 其他 {
    mListView.smoothScrollToPosition(firstVisiblePosition  -  scrollableItems);
    mListView.setSelection(0);
}
mListView.clearFocus();
 

创意

OK,我们可以改变平滑的假象逻辑:第一setSelection(),然后平稳地滚动(我们滚动到列表顶部的第一个项目)

  INT firstVisiblePosition = mListView.getFirstVisiblePosition();

    如果(firstVisiblePosition< scrollableItems){
        mListView.smoothScrollToPosition(0);
    } 其他 {
        mListView.setSelection(scrollableItems);
        mListView.smoothScrollToPosition(0);
    }
    mListView.clearFocus();
 

解决方案

 最终的ListView ListView控件= ...;
查看listItemView = ...;
listView.smoothScrollBy(listItemView.getHeight()* NUMBER_OF_VIEWS,
    工期* 2);
listView.postDelayed(新的Runnable(){
    公共无效的run(){
        listView.smoothScrollBy(0,0); //过头停止列表视图。
        listView.setSelection(0);
    }
},持续时间);
 

当然,滚动等的方向将需要调整为你的使用情况(转到列表的顶部)

修改:旧的解决方案可能超调,如果滚动速度过高,smoothScrollBy(0,0),将正确的,并立即设置选择之前停止平滑滚动

Scope

need to scroll to certain position smoothly and then "jump" to another positoin with setSelection(anotherPosition). This is done to create an illusion of smooth scrolling of (e.g.) 100 items in ListView. smoothScrollToPosition(100) lasts too much, you know.

Problem

setSelection() doesn't wait till smoothScrollToPosition finishes it's work, so setSelection() is called immediately and user see quick jumping only;

Code

private final int scrollableItems = 20;

int firstVisiblePosition = mListView.getFirstVisiblePosition();
if (firstVisiblePosition < scrollableItems) {
    mListView.smoothScrollToPosition(0);
} else {
    mListView.smoothScrollToPosition(firstVisiblePosition - scrollableItems);
    mListView.setSelection(0);
}
mListView.clearFocus();

Idea

OK, we could change logic of smoothness illusion: first setSelection(), then scroll smoothly (we're scrolling to very first item on top of the list)

    int firstVisiblePosition = mListView.getFirstVisiblePosition();

    if (firstVisiblePosition < scrollableItems) {
        mListView.smoothScrollToPosition(0);
    } else {
        mListView.setSelection(scrollableItems);
        mListView.smoothScrollToPosition(0);
    }
    mListView.clearFocus();

解决方案

final ListView listView = ...;
View listItemView = ...;
listView.smoothScrollBy(listItemView.getHeight() * NUMBER_OF_VIEWS, 
    DURATION * 2);
listView.postDelayed(new Runnable() {
    public void run() {
        listView.smoothScrollBy(0, 0); // Stops the listview from overshooting.
        listView.setSelection(0);
    }
}, DURATION);

Of course, direction of the scroll etc. would need to be adjusted for your use case (go to the top of the list)

EDIT: Old solution could overshoot if the velocity of the scroll was too high, smoothScrollBy(0,0) will stop the smooth scrolling before setting the selection properly and immediately.

 
精彩推荐
图片推荐