首页 > 解决方案 > Android - 在视图调整大小时保持 recylerview 选定的项目

问题描述

我在一个活动中有两个 FrameLayouts。第一个包含recylerview 或listview,第二个包含不同的控件,具体取决于情况。

当控件添加到第二个框架布局时,我想保持用户可以看到 recylerview 中的选定项目。

选择项目时,我在Recylerview布局管理器上使用卷轴定位,但是在屏幕上可见新控件之前,这是调用的,因此RecyLerview中的所选项目可以脱离屏幕。

有谁知道我怎么能做到这一点?

标签: android

解决方案


在大多数情况下,我设法让这个工作。在 onCreateView 中使用以下代码。希望这对其他人有帮助

view.addOnLayoutChangeListener(new View.OnLayoutChangeListener()
    {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
        {
            if ((bottom < oldBottom) && currentlyPlayingFile != null && currentlyPlayingFilePos != -1)
            {
                if (currentlyPlayingFilePos > (mFileData.length * 0.75))
                {
                    int moveAmount = oldBottom - bottom;
                    if (currentlyPlayingFilePos == (mFileData.length - 1))
                        moveAmount += 35;

                    mRecyclerView.scrollBy(0, moveAmount);
                }
                else
                {
                    mRecyclerView.scrollToPosition(currentlyPlayingFilePos);
                }
            }
        }
    });

推荐阅读