Android setReverseLayout中的RecyclerView滚动至顶部(或底部)

Android setReverseLayout中的RecyclerView滚动至顶部(或底部),android,android-recyclerview,Android,Android Recyclerview,我为这个可能会引起误解的标题道歉,但我不知道该怎么说。如果其他人认为他们可以想出更好的东西,请随意编辑 我有一个用Firebase记录填充的RecyclerView。为了使最新记录显示在列表的顶部,我已按照建议将RecvlerView设置为setReverseLayout(true)和setStackFromEnd(true)。当屏幕空间中的项目数量合适时,这非常有效-其余项目平滑向下滚动,为淡入的新项目提供空间,如下所示 问题是,当现有项目占用所有屏幕空间时,新项目被添加到其他项目的“上方”

我为这个可能会引起误解的标题道歉,但我不知道该怎么说。如果其他人认为他们可以想出更好的东西,请随意编辑

我有一个用Firebase记录填充的RecyclerView。为了使最新记录显示在列表的顶部,我已按照建议将RecvlerView设置为
setReverseLayout(true)
setStackFromEnd(true)
。当屏幕空间中的项目数量合适时,这非常有效-其余项目平滑向下滚动,为淡入的新项目提供空间,如下所示

问题是,当现有项目占用所有屏幕空间时,新项目被添加到其他项目的“上方”,并且RecyclerView有效地扩展到屏幕顶部上方,因此要查看新项目,我需要手动下拉或向下滚动列表。下面是另一个GIF

如何克服这一问题?

这可能有助于:

adapter.notifyItemInserted(position);
recyclerView.scrollToPosition(position);

如果你在最顶端,这将向上滚动,如果你在某个中间位置,从恼人的跳跃中保存,则一切保持原样。

你能分享这个示例链接吗。。。这是非常重要的useful@GunduBandagar对不起,你想分享什么链接?你在做上面的代码,我要这个老板……)欢迎来到Stackoverflow。最好在答案中添加一些描述,以说明您提出的代码是如何解决问题的。
  mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            int testimonycount = mAdapter.getItemCount();
            int lastVisiblePosition =
                    linearLayoutManager.findLastCompletelyVisibleItemPosition();
            // If the recycler view is initially being loaded or the
            // user is at the bottom of the list, scroll to the bottom
            // of the list to show the newly added message.
            if (lastVisiblePosition == -1 ||
                    (positionStart >= (testimonycount - 1) &&
                            lastVisiblePosition == (positionStart - 1))) {
                recipeRecyclerview.scrollToPosition(positionStart);
            }
        }
  mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            int testimonycount = mAdapter.getItemCount();
            int lastVisiblePosition =
                    linearLayoutManager.findLastCompletelyVisibleItemPosition();
            // If the recycler view is initially being loaded or the
            // user is at the bottom of the list, scroll to the bottom
            // of the list to show the newly added message.
            if (lastVisiblePosition == -1 ||
                    (positionStart >= (testimonycount - 1) &&
                            lastVisiblePosition == (positionStart - 1))) {
                recipeRecyclerview.scrollToPosition(positionStart);
            }
        }