Android 仿抖音评论列表

仿抖音评论列表

https://github.com/sunjian0/Comment

突然来需求,需要协助公司其他部门项目,分给我们部门评论的功能部分。本来寻思,一个评论能有多少东西,写的时候发现细节还挺多。所以此处记录一下。

使用的BaseRecyclerViewAdapterHelper3.0版本的BaseNodeAdapter,功能包括:

  1. 一级列表

  1. 二级列表

  1. 评论内容的部分显示、全部显示

  1. 评论功能、回复评论功能

    以下是一个简单的 Android 评论列表的实现代码,仅供参考: 1. 定义评论列表的布局文件 comment_list_item.xml ```xml <LinearLayout android:id="@+id/comment_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 头像、昵称、时间等信息的布局 --> <TextView android:id="@+id/comment_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textColor="@android:color/black" android:textSize="16sp" /> <!-- 回复列表的布局 --> </LinearLayout> ``` 2. 定义回复列表的布局文件 reply_list_item.xml ```xml <LinearLayout android:id="@+id/reply_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 头像、昵称、时间等信息的布局 --> <TextView android:id="@+id/reply_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> ``` 3. 定义评论列表的适配器 CommentListAdapter.java ```java public class CommentListAdapter extends RecyclerView.Adapter<CommentListAdapter.CommentViewHolder> { private List<Comment> mCommentList; public CommentListAdapter(List<Comment> commentList) { mCommentList = commentList; } @NonNull @Override public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_list_item, parent, false); return new CommentViewHolder(view); } @Override public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) { Comment comment = mCommentList.get(position); holder.mCommentContent.setText(comment.getContent()); // 设置回复列表 List<Reply> replyList = comment.getReplyList(); if (replyList != null && replyList.size() > 0) { holder.mReplyLayout.setVisibility(View.VISIBLE); holder.mReplyList.removeAllViews(); for (Reply reply : replyList) { View view = LayoutInflater.from(holder.itemView.getContext()).inflate(R.layout.reply_list_item, holder.mReplyList, false); // 设置回复信息 TextView replyContent = view.findViewById(R.id.reply_content); replyContent.setText(reply.getContent()); holder.mReplyList.addView(view); } } else { holder.mReplyLayout.setVisibility(View.GONE); } } @Override public int getItemCount() { return mCommentList == null ? 0 : mCommentList.size(); } static class CommentViewHolder extends RecyclerView.ViewHolder { TextView mCommentContent; LinearLayout mReplyLayout; LinearLayout mReplyList; CommentViewHolder(@NonNull View itemView) { super(itemView); mCommentContent = itemView.findViewById(R.id.comment_content); mReplyLayout = itemView.findViewById(R.id.reply_layout); mReplyList = itemView.findViewById(R.id.reply_list); } } } ``` 4. 在 Activity 中使用评论列表 ```java public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private CommentListAdapter mAdapter; private List<Comment> mCommentList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化评论列表 initCommentList(); // 设置 RecyclerView mRecyclerView = findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mAdapter = new CommentListAdapter(mCommentList); mRecyclerView.setAdapter(mAdapter); } private void initCommentList() { for (int i = 0; i < 10; i++) { Comment comment = new Comment(); comment.setContent("这是一条评论" + i); // 添加回复列表 List<Reply> replyList = new ArrayList<>(); for (int j = 0; j < 3; j++) { Reply reply = new Reply(); reply.setContent("这是一条回复" + j); replyList.add(reply); } comment.setReplyList(replyList); mCommentList.add(comment); } } } ``` 以上代码仅为实现评论列表的基本思路,具体实现还需要根据自己的需求进行调整。
    评论 1
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值