简体   繁体   English

如何在 Android 中自动向上滚动回收站视图?

[英]How to auto scroll up Recycler view in Android?

I have developed some chat application where i can send and received messages.我开发了一些聊天应用程序,我可以在其中发送和接收消息。 But the problem is whenever i send or receive messages the recycler view is not scrolling to top so that messages gets diplayed above the keypad.但问题是每当我发送或接收消息时,回收站视图都不会滚动到顶部,因此消息会显示在键盘上方。

I have used the above mentioned recycler view android.support.v7.widget.RecyclerView .我使用了上面提到的回收器视图android.support.v7.widget.RecyclerView

On updating recyclerview try to run this code : 在更新recyclerview时尝试运行此代码:

recyclerView.post(new Runnable() {
    @Override
    public void run() {
        // Call smooth scroll  
        recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1);                                 
    }
});

I use the following code so that the scroll is automatic until the last position: 我使用以下代码,以便滚动自动,直到最后一个位置:

recyclerView.smoothScrollToPosition(adapter.getItemCount());

I implement to add a new item to the list. 我实现了向列表添加新项目。

Try the bellow method for auto scroll to your particular position. 尝试使用波纹管方法自动滚动到您的特定位置。

adapter = new DeliveriesDateRecycleAdapter(getActivity(),sampleDatedate, position);
                recyclerviewDates.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                recyclerviewDates.smoothScrollToPosition(position);

Note : position - the position of the list that you want to show. 注意 :position - 要显示的列表的位置。

Have you tried RecyclerView.srollTo(int x, int y) ? 你试过RecyclerView.srollTo(int x, int y)吗?

Edit: Try RecyclerView.scrollTo(0, 0) directly after you have send or received the last message. 编辑:在发送或接收最后一条消息后直接尝试RecyclerView.scrollTo(0, 0)

try with following method for auto scroll to any position....you can change argument of the "notifyItemChanged" method.尝试使用以下方法自动滚动到任何位置....您可以更改“notifyItemChanged”方法的参数。

 public void printer(String message){
    block block=new block();
    block.details=message;

    blocks.add(block);

    blockAdapter.notifyItemChanged(0);
    recyclerView.scrollToPosition(blocks.size()-1);
   // recyclerView.scrollTo(blocks.size()-1,blocks.size());
}

I was using horizontal recycler view with linear layout manager,accepted solution didn't work for me我正在使用带有线性布局管理器的水平回收器视图,接受的解决方案对我不起作用

But below solution worked,但以下解决方案有效,

(recycler.layoutManager as? LinearLayoutManager)?.scrollToPositionWithOffset(position, 0)

If you want to take your user directly to the latest item without scroll animation then use this:如果您想在不滚动animation 的情况下将用户直接带到最新项目,请使用以下命令:

recyclerView.scrollToPosition(adapter.getItemCount()-1);

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

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