package cn.shangyu.gdxzExpert.adapter; import android.content.Context; import android.content.DialogInterface; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import java.util.ArrayList; import java.util.List; import cn.shangyu.gdxzExpert.R; import cn.shangyu.gdxzExpert.bean.Child; import cn.shangyu.gdxzExpert.bean.NewCommentBean; import cn.shangyu.gdxzExpert.utils.SharePrefUtil; import cn.shangyu.gdxzExpert.utils.Url; import cn.shangyu.gdxzExpert.widget.ECAlertDialog; /** * Author: Moos * E-mail: moosphon@gmail.com * Date: 18/4/20. * Desc: 评论与回复列表的适配器 */ public class CommentExpandAdapter1 extends BaseExpandableListAdapter { private static final String TAG = "CommentExpandAdapter"; private List commentBeanList; private List replyBeanList; private Context context; private int pageIndex = 1; private String puuid; ECAlertDialog buildAlert; public CommentExpandAdapter1(Context context, List commentBeanList) { this.context = context; this.commentBeanList = commentBeanList; puuid = SharePrefUtil.getString(context, "uuid", ""); } @Override public int getGroupCount() { return commentBeanList.size(); } @Override public int getChildrenCount(int i) { if(commentBeanList.get(i).getChildList() == null){ return 0; }else { return commentBeanList.get(i).getChildList().size()>0 ? commentBeanList.get(i).getChildList().size():0; } } @Override public Object getGroup(int i) { return commentBeanList.get(i); } @Override public Object getChild(int i, int i1) { return commentBeanList.get(i).getChildList().get(i1); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return getCombinedChildId(groupPosition, childPosition); } @Override public boolean hasStableIds() { return true; } boolean isLike = false; @Override public View getGroupView(final int groupPosition, boolean isExpand, View convertView, ViewGroup viewGroup) { final GroupHolder groupHolder; if(convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.item_comment_new_p, viewGroup, false); groupHolder = new GroupHolder(convertView); convertView.setTag(groupHolder); }else { groupHolder = (GroupHolder) convertView.getTag(); } if(puuid.equals(commentBeanList.get(groupPosition).getUser_uuid())) { groupHolder.iv_item_comment_delete.setVisibility(View.VISIBLE); groupHolder.item_comment_hf.setVisibility(View.GONE); } else { groupHolder.iv_item_comment_delete.setVisibility(View.GONE); groupHolder.item_comment_hf.setVisibility(View.VISIBLE); } if (!TextUtils.isEmpty(commentBeanList.get(groupPosition).getPhoto())) { RequestOptions requestOptions = new RequestOptions(); requestOptions.placeholder(R.drawable.icon_touxiang_persion_gray_big); requestOptions.error(R.drawable.icon_touxiang_persion_gray_big); Glide.with(context).load( Url.urlHtml + commentBeanList.get(groupPosition).getPhoto()).apply(requestOptions).into(groupHolder.logo); } else { Glide.with(context).load(R.drawable.icon_touxiang_persion_gray_big).into(groupHolder.logo); } // if (!TextUtils.isEmpty(commentBeanList.get(groupPosition).getPhoto())) { // ImageLoader // .getInstance() // .displayImage( // Url.urlHtml + commentBeanList.get(groupPosition).getPhoto(), // groupHolder.logo, // ImageOptions // .getImageOptions(R.drawable.icon_touxiang_persion_gray_big)); // } groupHolder.tv_name.setText(commentBeanList.get(groupPosition).getName()); groupHolder.tv_time.setText(commentBeanList.get(groupPosition).getCreate_date()); groupHolder.tv_content.setText(commentBeanList.get(groupPosition).getContent()); groupHolder.iv_item_comment_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { buildAlert = ECAlertDialog.buildAlert( context, "确定删除评论吗?", "取消", "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {// 取消 // TODO Auto-generated method stub buildAlert.dismiss(); } }, new DialogInterface.OnClickListener() {// 确定 @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub buildAlert.dismiss(); } }); buildAlert.setTitle("删除评论"); buildAlert.setCanceledOnTouchOutside(false); buildAlert.show(); } }); return convertView; } @Override public View getChildView(final int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) { final ChildHolder childHolder; if(convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.item_comment_new,viewGroup, false); childHolder = new ChildHolder(convertView); convertView.setTag(childHolder); } else { childHolder = (ChildHolder) convertView.getTag(); } if(puuid.equals(commentBeanList.get(groupPosition).getUser_uuid())) { childHolder.iv_item_comment_delete.setVisibility(View.VISIBLE); } else { childHolder.iv_item_comment_delete.setVisibility(View.GONE); } // if (!TextUtils.isEmpty(commentBeanList.get(groupPosition).getChildList().get(childPosition).getPhoto())) { // ImageLoader // .getInstance() // .displayImage( // Url.urlHtml + commentBeanList.get(groupPosition).getChildList().get(childPosition).getPhoto(), // childHolder.logo, // ImageOptions // .getImageOptions(R.drawable.icon_touxiang_persion_gray_big)); // } if (!TextUtils.isEmpty(commentBeanList.get(groupPosition).getChildList().get(childPosition).getPhoto())) { RequestOptions requestOptions = new RequestOptions(); requestOptions.placeholder(R.drawable.icon_touxiang_persion_gray_big); requestOptions.error(R.drawable.icon_touxiang_persion_gray_big); Glide.with(context). load( Url.urlHtml + commentBeanList.get(groupPosition).getChildList().get(childPosition).getPhoto()) .apply(requestOptions).into(childHolder.logo); } else { Glide.with(context).load(R.drawable.icon_touxiang_persion_gray_big).into( childHolder.logo); } String replyUser = commentBeanList.get(groupPosition).getChildList().get(childPosition).getName(); if(!TextUtils.isEmpty(replyUser)){ childHolder.tv_name.setText(replyUser + ":"); }else { childHolder.tv_name.setText("无名"+":"); } childHolder.tv_content.setText(commentBeanList.get(groupPosition).getChildList().get(childPosition).getContent()); return convertView; } @Override public boolean isChildSelectable(int i, int i1) { return true; } private class GroupHolder{ private ImageView logo; private ImageView iv_item_comment_delete; private TextView tv_name, tv_content, tv_time,item_comment_hf; // private ImageView iv_like; public GroupHolder(View view) { logo = view.findViewById(R.id.iv_patient_touxiang); tv_content = (TextView) view.findViewById(R.id.item_comment_content); tv_name = (TextView) view.findViewById(R.id.item_comment_name); tv_time = (TextView) view.findViewById(R.id.item_comment_time); item_comment_hf=view.findViewById(R.id.item_comment_hf); iv_item_comment_delete=view.findViewById(R.id.iv_item_comment_delete); // iv_like = (ImageView) view.findViewById(R.id.comment_item_like); } } private class ChildHolder{ private ImageView logo; private ImageView iv_item_comment_delete; private TextView tv_name, tv_content, tv_time; public ChildHolder(View view) { logo = view.findViewById(R.id.iv_patient_touxiang); tv_content = (TextView) view.findViewById(R.id.item_comment_content); tv_name = (TextView) view.findViewById(R.id.item_comment_name); tv_time = (TextView) view.findViewById(R.id.item_comment_time); iv_item_comment_delete=view.findViewById(R.id.iv_item_comment_delete); } } /** * by moos on 2018/04/20 * func:评论成功后插入一条数据 * @param commentDetailBean 新的评论数据 */ public void addTheCommentData(NewCommentBean commentDetailBean){ if(commentDetailBean!=null){ commentBeanList.add(commentDetailBean); notifyDataSetChanged(); }else { throw new IllegalArgumentException("评论数据为空!"); } } /** * by moos on 2018/04/20 * func:回复成功后插入一条数据 * @param replyDetailBean 新的回复数据 */ public void addTheReplyData(Child replyDetailBean, int groupPosition){ if(replyDetailBean!=null){ Log.e(TAG, "addTheReplyData: >>>>该刷新回复列表了:"+replyDetailBean.toString() ); if(commentBeanList.get(groupPosition).getChildList() != null ){ commentBeanList.get(groupPosition).getChildList().add(replyDetailBean); }else { List replyList = new ArrayList<>(); replyList.add(replyDetailBean); commentBeanList.get(groupPosition).setChildList(replyList); } notifyDataSetChanged(); }else { throw new IllegalArgumentException("回复数据为空!"); } } /** * by moos on 2018/04/20 * func:添加和展示所有回复 * @param replyBeanList 所有回复数据 * @param groupPosition 当前的评论 */ private void addReplyList(List replyBeanList, int groupPosition){ if(commentBeanList.get(groupPosition).getChildList() != null ){ commentBeanList.get(groupPosition).getChildList().clear(); commentBeanList.get(groupPosition).getChildList().addAll(replyBeanList); }else { commentBeanList.get(groupPosition).setChildList(replyBeanList); } notifyDataSetChanged(); } }