Implementing a Two-Level Comment Feature in Android Using Nested RecyclerViews
This article explains how to build a two-level comment system for an Android app by designing a comment database schema, creating DAO and repository layers, and using nested RecyclerViews with adapters to display primary comments and their replies, including code snippets and layout illustrations.
During Android app development, a comment section with both primary comments and replies is essential; this article describes the author's approach to implementing such a feature using a nested RecyclerView structure.
Database design includes a Comment entity with fields such as id (primary key), newsId , number (user ID), content , time , level (1 for top‑level, 2 for replies), replyNumber , and replyId (used only for level‑2 comments).
The Data Access Object (DAO) encapsulates typical SQL operations: inserting a comment, deleting by ID, retrieving all comments for an article, retrieving a user's comments, and fetching a comment by its ID.
Layout consists of three main parts: the article detail page showing a RecyclerView for top‑level comments, an adapter layout that itself contains another RecyclerView for replies, and a simple second‑level adapter layout built from a few TextView components.
Code logic is handled in the ViewModel, where all comments for a given article are loaded into a MutableLiveData<List<CommentInfo>> . The primary adapter receives only comments with level == 1 , while a filtered list of level == 2 comments is passed to the reply adapter. Binding the ViewHolder involves counting top‑level items, filtering related replies, and invoking callbacks for actions such as replying.
var comments = MutableLiveData<List<CommentInfo>>()
comments.value = commentStoreRepository.getCommentsByNewId(newsId)The article also shows screenshots of the final UI, demonstrating both the overall comment list and the “My Comments” view, and notes a minor issue where an empty replyNumber prevented the article author’s details from displaying.
In conclusion, the author successfully created a functional two‑level comment system, inviting feedback and reminding readers to credit the source when reposting.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.