简体   繁体   English

recyclerview.scrollToPosition(adapterarray.size()-1) 导致项目以相反的顺序出现

[英]recyclerview.scrollToPosition(adapterarray.size()-1) causes to bring the items in reversed order

I'm using Android Studio.我正在使用 Android Studio。

My recylview and its adapter works very well.我的 recylview 及其适配器运行良好。 My recyclerview can only 6 rows because of its design.我的 recyclerview 因为它的设计只能有 6 行。 So the items from 1 to 6 are showing perfectly.所以从 1 到 6 的项目显示完美。 When the 7th item added, it stays out of recyclerView borders so I should manualy scroll it to see the 7th line.添加第 7 项时,它不在 recyclerView 边框之外,因此我应该手动滚动它以查看第 7 行。 I decided to make it autoscroll to the last item line of the recyclerView.我决定让它自动滚动到 recyclerView 的最后一个项目行。 That means last 6 lines (say there is 10 items, so the lines 5,6,7,8,9,10 should be shown automatically.这意味着最后 6 行(假设有 10 个项目,因此应自动显示第 5、6、7、8、9、10 行。

In order to accomplish this, I have added the following line just after the line recyclerview.setAdapter(adapter);为了做到这一点,我在recyclerview.setAdapter(adapter);行之后添加了以下行

recyclerView.scrolltoposition(adapterarray.size()-1)

But now, the items are showing in reverse order.但是现在,这些项目以相反的顺序显示。 The last item is at the top of the recylerVeiw.最后一项位于 recylerVeiw 的顶部。 It seems that scrolltoposition sorts not only recyclerVievitself but also adapter.似乎 scrolltoposition 不仅对 recyclerVievitself 排序,而且对适配器排序。

How can I simply perform an auto scroll after adding new items in recyclerView if it is more than6 lines?如果超过 6 行,在 recyclerView 中添加新项目后如何简单地执行自动滚动?

Try something like that:尝试这样的事情:

recyclerView.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) {
                    recyclerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                  recyclerView.smoothScrollToPosition(adapter.getItemCount());
                        }
                    }, 100);
                }
            }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM