简体   繁体   English

Android 聊天,添加新消息显示

[英]Android chat, add new message showing

I'm working on chat activity with firebase and recyclerview.我正在与 firebase 和 recyclerview 进行聊天活动。 I've encountered little issue.我遇到了小问题。 When new message appears im using notifyItemInserted and then smoothScrollToPosition.当新消息出现时,我使用 notifyItemInserted 然后 smoothScrollToPosition。 When few first messages come (till screen is not full filled with messages) they are showing really smooth and it looks like same as in Facebook Messenger, but when it comes to scrolling to new message which is out of height our screen it pops up in blink of an eye which looks not good.当很少有第一条消息出现时(直到屏幕没有填满消息),它们显示非常流畅,看起来与 Facebook Messenger 中的相同,但是当滚动到超出高度的新消息时,我们的屏幕会弹出转眼里面看起来不太好。 I thought it has something to do with view recycling by recyclerview or with that smoothscrolling, but I dont know how to handle this.我认为这与 recyclerview 的视图回收或平滑滚动有关,但我不知道如何处理。

Some asked code:一些询问的代码:

    mMessagesList.add(message);
    mChatRecyclerAdapter.notifyItemInserted(mChatRecyclerAdapter.getItemCount()-1);
    rvChatMessages.smoothScrollToPosition(mChatRecyclerAdapter.getItemCount()-1);

Could You guys share some tips with me?各位大神可以和我分享一些技巧吗? Thanks谢谢

There is lots of unknown in your question but generally speaking if you are using RecyclerView (as you mentioned), you could improve your UI experience by using reverseLayout set to "true" in your LinearLayoutManager (my guess).您的问题中有很多未知数,但一般来说,如果您使用 RecyclerView (如您所述),您可以通过在LinearLayoutManager中使用reverseLayout设置为“true”来改善您的 UI 体验(我猜)。 Doing so would make your messages appear at the bottom of the list.这样做会使您的消息出现在列表的底部。 This still has its own issues if you do paging, depending on how you do paging etc.如果您进行分页,这仍然有其自身的问题,具体取决于您如何进行分页等。


An update (key points from chat below):更新(以下聊天的要点):

  1. Use reverse LinearLayoutManager (option of constructor) - not the same as stackFromBottom使用反向 LinearLayoutManager(构造函数的选项) - 与 stackFromBottom 不同
  2. Add data to beginning of data collection (.add(0, ...)将数据添加到数据收集的开头 (.add(0, ...)
  3. Call notifyItemInserted(0) on adapter在适配器上调用 notifyItemInserted(0)

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

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