gpt4 book ai didi

java - 检查 smoothScrollToPosition 何时完成

转载 作者:太空狗 更新时间:2023-10-29 13:47:58 30 4
gpt4 key购买 nike

我想检查 smoothScrollToPosition 何时完成滚动回到 recyclerview 的第一项。我试过这样做,只有在 smoothScrollToPosition 仍在滚动时才有效:

recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView,new RecyclerView.State(), 0);
if (!recyclerView.getLayoutManager().isSmoothScrolling()) {
Log.d(TAG, "Scrolling has ended.");
}

最佳答案

我使用 onScrollStateChanged(RecyclerView recyclerView, int newState) 方法来跟踪滚动状态。启动滚动的方法如下所示:

private void scrollToPosition(int position){
recyclerView.removeOnScrollListener(onScrollListener);
recyclerView.addOnScrollListener(onScrollListener);
recyclerView.smoothScrollToPosition(position);
}

这是监听器:

RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
switch (newState) {
case SCROLL_STATE_IDLE:
//we reached the target position
recyclerView.removeOnScrollListener(this);
break;
}
}
};

因此,当 recyclerView 到达 SCROLL_STATE_IDLE 时,这意味着它已经完成滚动。不要忘记在此状态下移除监听器,因此它不会在下一次滚动时触发。

关于java - 检查 smoothScrollToPosition 何时完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51261327/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com