129 lines
4.6 KiB
Java
129 lines
4.6 KiB
Java
package com.qxcm.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.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.blankj.utilcode.util.TimeUtils;
|
|
import com.qxcm.moduleutil.R;
|
|
import com.qxcm.moduleutil.bean.CommentBean;
|
|
|
|
import java.util.List;
|
|
|
|
public class ReplyAdapter extends RecyclerView.Adapter<ReplyAdapter.ReplyViewHolder> {
|
|
public interface OnReplyClickListener {
|
|
void onReplyClick(CommentBean.CommentDetailsBean.Replies reply, int position);
|
|
}
|
|
// 添加回调接口
|
|
public interface OnReplyLongClickListener {
|
|
void onReplyLongClick(CommentBean.CommentDetailsBean.Replies reply, int position);
|
|
}
|
|
|
|
private OnReplyLongClickListener longClickListener;
|
|
|
|
public void setOnReplyLongClickListener(OnReplyLongClickListener listener) {
|
|
this.longClickListener = listener;
|
|
}
|
|
private OnReplyClickListener listener;
|
|
|
|
// 设置监听器的方法
|
|
public void setOnReplyClickListener(OnReplyClickListener listener) {
|
|
this.listener = listener;
|
|
}
|
|
private List<CommentBean.CommentDetailsBean.Replies> replyList;
|
|
private boolean isExpanded = false; // 是否展开显示所有评论
|
|
|
|
public ReplyAdapter(List<CommentBean.CommentDetailsBean.Replies> replyList) {
|
|
this.replyList = replyList;
|
|
}
|
|
|
|
public void updateData(List<CommentBean.CommentDetailsBean.Replies> newReplyList) {
|
|
replyList.clear();
|
|
replyList.addAll(newReplyList);
|
|
notifyDataSetChanged();
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public ReplyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_reply, parent, false);
|
|
return new ReplyViewHolder(view);
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(@NonNull ReplyViewHolder holder, int position) {
|
|
CommentBean.CommentDetailsBean.Replies reply = replyList.get(position);
|
|
if (!isExpanded && position >= 2) {
|
|
return; // 不绑定数据
|
|
}
|
|
holder.bind(reply,position, listener,longClickListener);
|
|
}
|
|
|
|
public void setExpanded(boolean expanded) {
|
|
isExpanded = expanded;
|
|
notifyDataSetChanged(); // 刷新适配器
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return isExpanded ? replyList.size() : Math.min(2, replyList.size());
|
|
}
|
|
|
|
class ReplyViewHolder extends RecyclerView.ViewHolder {
|
|
private TextView tvNickname;
|
|
private TextView tvContent;
|
|
private TextView btnReply;
|
|
private TextView tvTime;
|
|
private TextView btnShowAllReplies;
|
|
public ReplyViewHolder(@NonNull View itemView) {
|
|
super(itemView);
|
|
tvNickname = itemView.findViewById(R.id.tv_name);
|
|
tvContent = itemView.findViewById(R.id.tv_reply);
|
|
btnReply = itemView.findViewById(R.id.btn_reply);
|
|
tvTime = itemView.findViewById(R.id.tv_time);
|
|
// btnShowAllReplies = itemView.findViewById(R.id.btn_show_all_replies);
|
|
}
|
|
|
|
public void bind(CommentBean.CommentDetailsBean.Replies reply,int position, OnReplyClickListener listener,OnReplyLongClickListener longClickListener) {
|
|
|
|
tvNickname.setText(reply.getNickname()+":" +(reply.getReply_to_user()!=null?reply.getReply_to_user():""));
|
|
tvContent.setText(reply.getContent());
|
|
tvTime.setText(TimeUtils.millis2String(Long.parseLong(reply.getCreatetime()+"") * 1000));
|
|
btnReply.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (listener != null) {
|
|
listener.onReplyClick(reply, position);
|
|
}
|
|
}
|
|
});
|
|
|
|
// 添加长按监听
|
|
itemView.setOnLongClickListener(v -> {
|
|
if (longClickListener != null) {
|
|
longClickListener.onReplyLongClick(reply, position);
|
|
return true; // 消费事件
|
|
}
|
|
return false;
|
|
});
|
|
// 控制“全部评论...”按钮的显示
|
|
// btnShowAllReplies.setVisibility(position > 2 ? VISIBLE : GONE);
|
|
//
|
|
// btnShowAllReplies.setOnClickListener(v -> {
|
|
//// replyAdapter.setExpanded(true); // 展开所有评论
|
|
// notifyDataSetChanged(); // 刷新适配器
|
|
// btnShowAllReplies.setVisibility(GONE); // 隐藏按钮
|
|
// });
|
|
}
|
|
}
|
|
}
|
|
|