package com.xscm.moduleutil.adapter; import static android.view.View.GONE; import static android.view.View.VISIBLE; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.blankj.utilcode.util.TimeUtils; import com.xscm.moduleutil.R; import com.xscm.moduleutil.bean.CommentBean; import com.xscm.moduleutil.utils.MeHeadView; import com.xscm.moduleutil.utils.SpUtil; import java.util.ArrayList; import java.util.List; public class CommentAdapter extends RecyclerView.Adapter { public interface OnCommentInteractionListener { void onInputBoxShow(int id,String s,int position,String replyTo); void onDetaleClick(int id,int position); // 新增长按回调 void onCommentLongClick(CommentBean.CommentDetailsBean comment, CommentBean.CommentDetailsBean.Replies reply, int position); } private OnCommentInteractionListener listener; // 新增监听器引用 public void setOnCommentInteractionListener(OnCommentInteractionListener listener) { this.listener = listener; } private List commentList; private static FragmentManager fragmentManager = null; public CommentAdapter(List commentList) { this.commentList = commentList; } public void updateData(List newReplyList) { DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new CommentDiffCallback(this.commentList, newReplyList)); this.commentList.clear(); this.commentList.addAll(newReplyList); diffResult.dispatchUpdatesTo(this); } @NonNull @Override public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false); return new CommentViewHolder(view); } @Override public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) { CommentBean.CommentDetailsBean comment = commentList.get(position); holder.bind(comment, listener, position); } @Override public int getItemCount() { return commentList.size(); } static class CommentViewHolder extends RecyclerView.ViewHolder { private MeHeadView ivAvatar; private TextView tvNickname; private TextView tvContent; private RecyclerView rvReplies; private TextView btnReply,btnDetale; private TextView tvTime; private TextView btnShowAllReplies; private TextView tv_send; private EditText et_input; public CommentViewHolder(@NonNull View itemView) { super(itemView); ivAvatar = itemView.findViewById(R.id.iv_avatar); tvNickname = itemView.findViewById(R.id.tv_nickname); tvContent = itemView.findViewById(R.id.tv_content); rvReplies = itemView.findViewById(R.id.rv_replies); btnReply = itemView.findViewById(R.id.btn_reply); btnDetale=itemView.findViewById(R.id.btn_detale); tvTime = itemView.findViewById(R.id.tv_time); btnShowAllReplies = itemView.findViewById(R.id.btn_show_all_replies); tv_send= itemView.findViewById(R.id.tv_send); et_input= itemView.findViewById(R.id.et_input); // 设置子评论的适配器 LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext()); rvReplies.setLayoutManager(layoutManager); ReplyAdapter replyAdapter = new ReplyAdapter(new ArrayList<>()); rvReplies.setAdapter(replyAdapter); } public void bind(CommentBean.CommentDetailsBean comment, OnCommentInteractionListener listener, int position) { // 绑定主评论数据 tvNickname.setText(comment.getNickname()); tvContent.setText(comment.getContent()); ivAvatar.setData(comment.getAvatar(), "",""); // 加载用户头像(使用 Glide 或其他图片加载库) // Glide.with(itemView).load(comment.getAvatar()).into(ivAvatar); if (comment.getUser_id()==SpUtil.getUserId()){ btnDetale.setVisibility(View.VISIBLE); }else { btnDetale.setVisibility(View.GONE); } tvTime.setText(TimeUtils.millis2String(Long.parseLong(comment.getCreatetime()+"") * 1000)); // 绑定子评论数据 ReplyAdapter replyAdapter = (ReplyAdapter) rvReplies.getAdapter(); if (replyAdapter != null) { if (comment.getReplies() != null&& comment.getReplies().size() > 0) { rvReplies.setVisibility(VISIBLE); replyAdapter.updateData(comment.getReplies()); // 控制“全部评论...”按钮的显示 btnShowAllReplies.setVisibility(comment.getReplies().size() > 2 ? VISIBLE : GONE); btnShowAllReplies.setOnClickListener(v -> { replyAdapter.setExpanded(true); // 展开所有评论 btnShowAllReplies.setVisibility(GONE); // 隐藏按钮 }); replyAdapter.setOnReplyClickListener((reply, position1) -> { if (listener != null) { // 构造回复内容(如 @用户名) String replyContent = "@" + reply.getNickname() + " "; listener.onInputBoxShow(reply.getPid(), replyContent, getAdapterPosition(),reply.getReply_to()+""); } }); replyAdapter.setOnReplyLongClickListener((reply, replyPosition) -> { if (listener != null) { // 转发到Activity listener.onCommentLongClick(comment,reply, replyPosition); } }); }else { rvReplies.setVisibility(GONE); } } // 点击回复按钮 btnReply.setOnClickListener(v -> { if (listener != null) { // 传递需要的参数 listener.onInputBoxShow(comment.getId(), "", position,""); } }); btnDetale.setOnClickListener(v -> { if (listener != null) { listener.onDetaleClick(comment.getId(), position); // 点击删除按钮 } }); // if (comment.getUser_id()!= SpUtil.getUserId()){ // btnReply.setVisibility(GONE); // }else { // btnReply.setVisibility(VISIBLE); // } // 添加长按监听 itemView.setOnLongClickListener(v -> { if (listener != null) { listener.onCommentLongClick(comment, null,position); } return true; // 消费事件 }); } } static class CommentDiffCallback extends DiffUtil.Callback { private final List oldList; private final List newList; public CommentDiffCallback(List oldList, List newList) { this.oldList = oldList; this.newList = newList; } @Override public int getOldListSize() { return oldList.size(); } @Override public int getNewListSize() { return newList.size(); } @Override public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { return oldList.get(oldItemPosition).getId() == newList.get(newItemPosition).getId(); } @Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { return oldList.get(oldItemPosition).equals(newList.get(newItemPosition)); } } }