简体   繁体   中英

Android chat, add new message showing

I'm working on chat activity with firebase and recyclerview. I've encountered little issue. When new message appears im using notifyItemInserted and then 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. I thought it has something to do with view recycling by recyclerview or with that smoothscrolling, but I dont know how to handle this.

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). 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
  2. Add data to beginning of data collection (.add(0, ...)
  3. Call notifyItemInserted(0) on adapter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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