检查smoothScrollToPosition何时完成 [英] Check when smoothScrollToPosition has finished

查看:536
本文介绍了检查smoothScrollToPosition何时完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查smoothScrollToPosition完成何时滚动回到recyclerview的第一项.我尝试这样做,仅在smoothScrollToPosition仍在滚动时才起作用:

I would like to check when smoothScrollToPosition has finished scrolling back to the first item of recyclerview. I tried doing it like this which only works while smoothScrollToPosition is still scrolling:

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

推荐答案

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

I use onScrollStateChanged(RecyclerView recyclerView, int newState) method for tracking scrolling state. The method to initiate scroll looks like this:

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时,这意味着它已完成滚动.不要忘记在这种状态下删除侦听器,这样它将不会在下一次滚动时触发.

So, when recyclerView reaches SCROLL_STATE_IDLE, that means it has finished scrolling. Don't forget to remove listener in this state, so it won't trigger on the next scroll.

这篇关于检查smoothScrollToPosition何时完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆