平滑滚动不适用于 Android RecyclerView 的初始滚动 [英] Smooth Scroll Not Working on Initial Scroll for Android RecyclerView

查看:46
本文介绍了平滑滚动不适用于 Android RecyclerView 的初始滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款仅在一台运行 KitKat 的设备上运行的 Android 应用.

I am working on an Android app that runs on only one devicerunning KitKat.

我在其他物理平板电脑和 genymotion 上使用的 RecylerView 的平滑滚动功能不幸在它需要工作的一个设备上停止工作.

The smooth scrolling feature for a RecylerView I used that was working on other physical tablets and genymotion has unfortunately stopped working on the one device it needs to work on.

它没有滚动到某个位置,而是越过目标位置并一直滚动到底部,看起来很糟糕.

Instead of scrolling to a certain position it passes over the target position and scrolls all the way to the bottom and looks really bad.

我能够将错误追踪到 RecyclerView 类中的抽象 SmoothScroller.

I am able to track down the error to the abstract SmoothScroller in the RecyclerView class.

           if (getChildPosition(mTargetView) == mTargetPosition) {
                onTargetFound(mTargetView, recyclerView.mState, mRecyclingAction);
                mRecyclingAction.runIfNecessary(recyclerView);
                stop();
            } else {
                Log.e(TAG, "Passed over target position while smooth scrolling.");
                mTargetView = null;
            }

我使用的是在网上找到的 SnappingLinearLayoutManager,但用 Android 中的普通 LinearLayoutManager 替换了它,但仍然遇到同样的问题.

I was using a SnappingLinearLayoutManager that I found online, but swapped it out with the normal LinearLayoutManager from Android, and still am having the same problem.

列表有 7 个项目(用户一次可以看到 4 个),我滚动到第 5 个项目(位置 4)项目.

The list is 7 items long (user can see 4 at a time) and I scroll to the 5th item (position 4) item.

当我滚动到第三个时,我没有收到此错误.

When I scroll to the 3rd I don't receive this error.

此外,在我上下滚动列表一次后,错误停止发生.

Also after I scroll the list up and down once, the error stops happening.

我可以使用 layoutManager.scrollToPositionWithOffset();但我正在尝试使用平滑滚动动画来做到这一点.

这是我的一些代码和细节:

Here is some of my code and details:

private void setupMainRecyclerViewWithAdapter() {
    mainLayoutManager = new SnappingLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    mainListRecyclerView.setLayoutManager(mainLayoutManager);

    settingsMainListAdapter = new SettingsListAdapter(SettingsActivity.this,
            settingsPresenter.getSettingsItems(),
            settingsPresenter);

    mainListRecyclerView.setAdapter(settingsMainListAdapter);

    mainListRecyclerView.addItemDecoration(new BottomOffsetDecoration(EXTRA_VERTICAL_SCROLLING_SPACE));
}

@Override
public void scrollMainList(boolean listAtTop) {
    if(listAtTop) {
        mainListRecyclerView.smoothScrollToPosition(4);
        moveMainMoreButtonAboveList();
    } else {
        mainListRecyclerView.smoothScrollToPosition(0);
        moveMainMoreButtonBelowList();
    }
}

推荐答案

如果调用 recyclerView.smoothScrollToPosition(pos) 将在 UI 线程 上立即调用,如果recyclerViewAdapter 太忙,无法生成视图项,然后会错过 smoothScrollToPosition 的调用,因为 recyclerView> 没有数据来平滑滚动.因此最好通过 recyclerView.post() 在后台线程中执行此操作.通过调用它进入主线程队列并在其他待处理任务完成后执行.

If you call recyclerView.smoothScrollToPosition(pos) will be called immediately on the UI thread and if recyclerView's Adapter is too much busy to generating view items then the calling of smoothScrollToPosition will be missed then because recyclerView has no data to smooth scroll. So it's better to do that in a background thread by recyclerView.post(). By calling this it goes into the Main thread queue and gets executed after the other pending tasks are finished.

因此,您应该做这样的事情,这对我的情况有效:

Therefore you should do something like this which worked for my case:

recyclerView.post(new Runnable() {
    @Override
    public void run() {
        recyclerView.smoothScrollToPosition(pos);
    }
});

这篇关于平滑滚动不适用于 Android RecyclerView 的初始滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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