Comment section UI
The SDK delivers comments in order. How they are drawn on screen is up to your app, and that is where shaking lists, comments out of order and a list that pulls the reader down come from. This guide gives a pattern that stays smooth in a busy live stream or voice room.
What the SDK guarantees#
| Guarantee | What it means for your list |
|---|---|
| Messages from one sender arrive in the order sent, reliably. | Append each comment at the end as it arrives. Never sort by sentAt: it is the sender's clock. |
onMessage runs on the main thread (Android). | Update the adapter directly in the callback; a hop to another thread can reorder comments. |
Every message has a unique id. | Drop duplicates by it. |
| Your own messages are not echoed back. | Add your own comment to the list when you send it. |
Comments are sent with sendMessage; see Signaling. For comments that late joiners must see, see Chat.
Six rules#
- Fixed height. A
wrap_contentlist grows with every comment and shakes the layout. Give it a fixed or constrained height. - One row at a time.
notifyItemInserted, nevernotifyDataSetChanged()or a whole new list per comment. - Follow only at the bottom. If the user scrolled up to read, leave the list where it is and show a "↓ N new comments" button.
- The button jumps to the newest and hides. The user's own comment always scrolls to the bottom.
- Cap the list at about 200 rows and remove the oldest with
notifyItemRemoved(0). - Rows never change height after they are drawn: fixed-size avatars, badges and images.
Android (Java)#
// Adapter: append one row, drop duplicates, cap at 200boolean add(ChatLine line) {if (line.id != null && !seenIds.add(line.id)) return false;lines.add(line);notifyItemInserted(lines.size() - 1);if (lines.size() > 200) {ChatLine old = lines.remove(0);if (old.id != null) seenIds.remove(old.id);notifyItemRemoved(0);}return true;}// ActivityLinearLayoutManager lm = new LinearLayoutManager(this);lm.setStackFromEnd(true);chatList.setLayoutManager(lm);chatList.setAdapter(chat);chatList.setItemAnimator(null); // no per-row animation: it reads as shaking in a busy roomchatList.addOnScrollListener(new RecyclerView.OnScrollListener() {@Override public void onScrolled(@NonNull RecyclerView rv, int dx, int dy) {atBottom = !rv.canScrollVertically(1);if (atBottom) { unseen = 0; newPill.setVisibility(View.GONE); }}});newPill.setOnClickListener(v -> {unseen = 0;newPill.setVisibility(View.GONE);chatList.scrollToPosition(chat.last());});void onChatLine(boolean mine) {if (mine || atBottom) {unseen = 0;newPill.setVisibility(View.GONE);chatList.post(() -> chatList.smoothScrollToPosition(chat.last()));} else {unseen++;newPill.setText("↓ " + (unseen > 99 ? "99+" : unseen) + " new comments");newPill.setVisibility(View.VISIBLE);}}@Override public void onMessage(AppMessage m) { // main threadif (m.getText() == null) return;if (chat.add(ChatLine.text(m.getId(), m.getFrom(), m.getText()))) onChatLine(false);}void sendComment(String text) {engine.sendMessage(text);chat.add(ChatLine.text(null, me, text)); // not echoed back: add it yourselfonChatLine(true);}
Very busy rooms#
At more than about ten comments a second, collect incoming comments for 150 ms and add them together, then call onChatLine(false) once.
@Override public void onMessage(AppMessage m) {if (m.getText() == null) return;pending.add(ChatLine.text(m.getId(), m.getFrom(), m.getText()));main.removeCallbacks(flush);main.postDelayed(flush, 150);}
Common mistakes#
| Code | Effect | Fix |
|---|---|---|
wrap_content | The list grows and the screen shakes | Fixed height |
notifyDataSetChanged() | Every row redrawn: flicker, lost scroll position | notifyItemInserted |
| Always scrolling to the last row | A reader is pulled down | Only at the bottom, otherwise the button |
setReverseLayout(true) + insert at 0 | Comments in the wrong order | setStackFromEnd(true), append at the end |
Sorting by sentAt | Comments jump when a phone clock is wrong | Keep arrival order |
| Images that load into wrap_content views | Rows grow and the list jumps | Fixed-size image views |
Note
Use one comment component for live streams and voice rooms. Only the layout around it differs; the rules are the same.