|
|
@@ -0,0 +1,384 @@
|
|
|
+package com.xzjmyk.pm.activity.adapter;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
+import android.text.SpannableStringBuilder;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.widget.BaseAdapter;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.LinearLayout;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.android.volley.Response;
|
|
|
+import com.android.volley.VolleyError;
|
|
|
+import com.xzjmyk.pm.activity.MyApplication;
|
|
|
+import com.xzjmyk.pm.activity.R;
|
|
|
+import com.xzjmyk.pm.activity.bean.User;
|
|
|
+import com.xzjmyk.pm.activity.bean.circle.Comment;
|
|
|
+import com.xzjmyk.pm.activity.bean.circle.Praise;
|
|
|
+import com.xzjmyk.pm.activity.bean.circle.PublicMessage;
|
|
|
+import com.xzjmyk.pm.activity.helper.AvatarHelper;
|
|
|
+import com.xzjmyk.pm.activity.ui.base.BaseActivity;
|
|
|
+import com.xzjmyk.pm.activity.util.HtmlUtils;
|
|
|
+import com.xzjmyk.pm.activity.util.LinkMovementClickMethod;
|
|
|
+import com.xzjmyk.pm.activity.util.StringUtils;
|
|
|
+import com.xzjmyk.pm.activity.util.TimeUtils;
|
|
|
+import com.xzjmyk.pm.activity.util.ToastUtil;
|
|
|
+import com.xzjmyk.pm.activity.view.MyListView;
|
|
|
+import com.xzjmyk.pm.activity.volley.ObjectResult;
|
|
|
+import com.xzjmyk.pm.activity.volley.Result;
|
|
|
+import com.xzjmyk.pm.activity.volley.StringJsonObjectRequest;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by FANGlh on 2016/11/1.
|
|
|
+ */
|
|
|
+public class WorkDailyPaperAdapter extends BaseAdapter{
|
|
|
+ private static final int PRAISE_WHAT = 0x11;
|
|
|
+ private User user;
|
|
|
+ private List<PublicMessage> messages;
|
|
|
+ private Context context;
|
|
|
+ private CommentAdapter mAdapter;
|
|
|
+ private int pon;//点赞的对象
|
|
|
+ private Handler handler = new Handler() {
|
|
|
+ @Override
|
|
|
+ public void handleMessage(Message msg) {
|
|
|
+ String message = msg.getData().getString("result");
|
|
|
+ switch (msg.what) {
|
|
|
+ case PRAISE_WHAT:
|
|
|
+ JSONObject json = JSON.parseObject(message);
|
|
|
+ if (json.getIntValue("resultCode") != 1) return;
|
|
|
+ if (json.getString("data") == null) {//为取消点赞
|
|
|
+ messages.get(pon).setIsPraise(0);
|
|
|
+ messages.get(pon).setPraise(messages.get(pon).getPraise() - 1);
|
|
|
+ List<Praise> praises = messages.get(pon).getPraises();
|
|
|
+ for (int i = 0; i < praises.size(); i++) {
|
|
|
+ if (praises.get(i).getUserId().equals(MyApplication.getInstance().mLoginUser.getUserId())) {
|
|
|
+ praises.remove(i);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {//点赞
|
|
|
+ messages.get(pon).setIsPraise(1);
|
|
|
+ Praise bean = new Praise();
|
|
|
+ bean.setNickName(MyApplication.getInstance().mLoginUser.getNickName());
|
|
|
+ bean.setUserId(MyApplication.getInstance().mLoginUser.getUserId());
|
|
|
+ bean.setPraiseId(json.getString("data"));
|
|
|
+ messages.get(pon).getPraises().add(0, bean);
|
|
|
+ messages.get(pon).setPraise(messages.get(pon).getPraise() + 1);
|
|
|
+ }
|
|
|
+ notifyDataSetChanged();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ public WorkDailyPaperAdapter(Context context, List<PublicMessage> messages) {
|
|
|
+ this.context = context;
|
|
|
+ this.messages = messages;
|
|
|
+ this.user = MyApplication.getInstance().mLoginUser;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setData(List<PublicMessage> messages) {
|
|
|
+ this.messages = messages;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getCount() {
|
|
|
+ return messages == null ? 0 : messages.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getItem(int i) {
|
|
|
+ return messages.get(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long getItemId(int i) {
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public View getView(int i, View view, ViewGroup viewGroup) {
|
|
|
+ ViewHolder holder;
|
|
|
+ if (view == null) {
|
|
|
+ holder = new ViewHolder();
|
|
|
+ view = LayoutInflater.from(context).inflate(R.layout.item_activity_worklogs, null);
|
|
|
+ holder.avatar_img = (ImageView) view.findViewById(R.id.avatar_img);
|
|
|
+ holder.comment_tag = (LinearLayout) view.findViewById(R.id.comment_tag);
|
|
|
+ holder.name_tv = (TextView) view.findViewById(R.id.name_tv);
|
|
|
+ holder.content_tv = (TextView) view.findViewById(R.id.content_tv);
|
|
|
+ holder.time_tv = (TextView) view.findViewById(R.id.time_tv);
|
|
|
+ holder.like_tv = (TextView) view.findViewById(R.id.like_tv);
|
|
|
+ holder.command_tv = (TextView) view.findViewById(R.id.command_tv);
|
|
|
+ holder.command_lv = (MyListView) view.findViewById(R.id.command_lv);
|
|
|
+ view.setTag(holder);
|
|
|
+ } else {
|
|
|
+ holder = (ViewHolder) view.getTag();
|
|
|
+ }
|
|
|
+ bindData(holder, i);
|
|
|
+ return view;
|
|
|
+ }
|
|
|
+
|
|
|
+ private PublicMessage.Body body;//临时变量
|
|
|
+
|
|
|
+ private void bindData(ViewHolder holder, final int i) {
|
|
|
+ final PublicMessage message = messages.get(i);
|
|
|
+ holder.name_tv.setText(message.getNickName());//名字
|
|
|
+ AvatarHelper.getInstance().displayAvatar(message.getUserId(), holder.avatar_img, false);//头像设置
|
|
|
+ holder.time_tv.setText(TimeUtils.getFriendlyTimeDesc(context, (int) message.getTime()));//时间设置
|
|
|
+ body = message.getBody();
|
|
|
+ String text;
|
|
|
+ if (body == null || body.getText() == null) text = "";
|
|
|
+ else text = body.getText();
|
|
|
+ holder.content_tv.setText(text);
|
|
|
+ final List<Comment> comments = message.getComments();
|
|
|
+ holder.like_tv.setText(message.getPraise() + "");
|
|
|
+ final boolean isPraise = message.getIsPraise() != 1;
|
|
|
+ holder.command_tv.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ if (listener == null) return;
|
|
|
+ listener.onClick(i, message.getMessageId(), message.getUserId(), message.getNickName());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ holder.like_tv.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ praiseOrCancle(i, isPraise);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (comments == null || comments.size() <= 0) {
|
|
|
+ holder.comment_tag.setVisibility(View.GONE);
|
|
|
+ } else {
|
|
|
+ holder.comment_tag.setVisibility(View.VISIBLE);
|
|
|
+ }
|
|
|
+ holder.command_tv.setText(comments.size() + "");
|
|
|
+ mAdapter = new CommentAdapter(comments);
|
|
|
+ holder.command_lv.setAdapter(mAdapter);
|
|
|
+ }
|
|
|
+
|
|
|
+ class ViewHolder {
|
|
|
+ ImageView avatar_img; //头像
|
|
|
+ TextView name_tv,//名字
|
|
|
+ content_tv,//内容
|
|
|
+ time_tv, //发表时间
|
|
|
+ like_tv, //点赞按钮
|
|
|
+ command_tv;//评论
|
|
|
+ MyListView command_lv;
|
|
|
+ LinearLayout comment_tag;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //评论适配器
|
|
|
+ public class CommentAdapter extends BaseAdapter {
|
|
|
+ private int messagePosition;
|
|
|
+ private List<Comment> datas;
|
|
|
+
|
|
|
+ public CommentAdapter(int messagePosition, List<Comment> datas) {
|
|
|
+ this.messagePosition = messagePosition;
|
|
|
+ this.datas = datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ public CommentAdapter(List<Comment> datas) {
|
|
|
+ this.datas = datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getCount() {
|
|
|
+ return datas.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getItem(int position) {
|
|
|
+ return position;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long getItemId(int position) {
|
|
|
+ return position;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public View getView(final int position, View convertView, ViewGroup parent) {
|
|
|
+ ViewHolder holder = null;
|
|
|
+ if (convertView == null) {
|
|
|
+ holder = new ViewHolder();
|
|
|
+ convertView = LayoutInflater.from(context).inflate(R.layout.p_msg_comment_list_item, null);
|
|
|
+ holder.text_view = (TextView) convertView.findViewById(R.id.text_view);
|
|
|
+ convertView.setTag(holder);
|
|
|
+ } else {
|
|
|
+ holder = (ViewHolder) convertView.getTag();
|
|
|
+ }
|
|
|
+ final Comment comment = datas.get(position);
|
|
|
+ SpannableStringBuilder builder = new SpannableStringBuilder();
|
|
|
+ String showName = comment.getNickName();
|
|
|
+ UserClickableSpan.setClickableSpan(context, builder, showName, comment.getUserId());// 设置评论者的ClickSpanned
|
|
|
+ if (!TextUtils.isEmpty(comment.getToUserId()) && !TextUtils.isEmpty(comment.getToNickname())) {
|
|
|
+ builder.append(context.getString(R.string.replay));
|
|
|
+ String toShowName = comment.getToNickname();
|
|
|
+ UserClickableSpan.setClickableSpan(context, builder, toShowName, comment.getToUserId());// 设置被评论者的ClickSpanned
|
|
|
+ }
|
|
|
+
|
|
|
+ builder.append(":");
|
|
|
+ // 设置评论内容
|
|
|
+ String commentBody = comment.getBody();
|
|
|
+ if (!TextUtils.isEmpty(commentBody)) {
|
|
|
+ commentBody = StringUtils.replaceSpecialChar(comment.getBody());
|
|
|
+ CharSequence charSequence = HtmlUtils.transform200SpanString(commentBody.replaceAll("\n", "\r\n"),
|
|
|
+ true);
|
|
|
+ builder.append(charSequence);
|
|
|
+ }
|
|
|
+ holder.text_view.setText(builder);
|
|
|
+ holder.text_view.setLinksClickable(true);
|
|
|
+ holder.text_view.setMovementMethod(LinkMovementClickMethod.getInstance());
|
|
|
+ holder.text_view.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ //点击操作
|
|
|
+// if (comment.getUserId().equals(mLoginUserId)) {// 如果消息是我发的,那么就弹出删除和复制的对话框
|
|
|
+// showCommentLongClickDialog(messagePosition, position, CommentAdapter.this);
|
|
|
+// } else {// 弹出回复的框
|
|
|
+// String toShowName = getShowName(comment.getUserId(), comment.getNickName());
|
|
|
+// // 懒得写回调的,直接强转,以后如果不适用,可以写个接口回调
|
|
|
+// if (context instanceof BusinessCircleActivity) {
|
|
|
+// ((BusinessCircleActivity) mContext).showCommentEnterView(messagePosition,
|
|
|
+// comment.getUserId(), comment.getNickName(), toShowName);
|
|
|
+//
|
|
|
+// } else {
|
|
|
+// ((MainActivity) context).getBusinessCircleFragment().showCommentEnterView(messagePosition,
|
|
|
+// comment.getUserId(), comment.getNickName(), toShowName);
|
|
|
+// }
|
|
|
+// }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ holder.text_view.setOnLongClickListener(new View.OnLongClickListener() {
|
|
|
+ @Override
|
|
|
+ public boolean onLongClick(View v) {
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return convertView;
|
|
|
+ }
|
|
|
+
|
|
|
+ class ViewHolder {
|
|
|
+ TextView text_view;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private ButtonViewListener listener;
|
|
|
+
|
|
|
+ public void setButtonViewListener(ButtonViewListener listener) {
|
|
|
+ this.listener = listener;
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface ButtonViewListener {
|
|
|
+ void onClick(int i, String messageId, String userId, String nickName);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 赞或者取消赞
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @param isPraise
|
|
|
+ */
|
|
|
+// private void praiseOrCancle(final int position, final boolean isPraise) {
|
|
|
+// pon = position;
|
|
|
+// //参数
|
|
|
+// final PublicMessage message = messages.get(position);
|
|
|
+// String requestUrl = null;
|
|
|
+// if (isPraise) {
|
|
|
+// requestUrl = MyApplication.getInstance().getConfig().MSG_PRAISE_ADD;
|
|
|
+// } else {
|
|
|
+// requestUrl = MyApplication.getInstance().getConfig().MSG_PRAISE_DELETE;
|
|
|
+// }
|
|
|
+// HashMap<String, Object> params = new HashMap<>();
|
|
|
+// params.put("access_token", MyApplication.getInstance().mAccessToken);
|
|
|
+// params.put("messageId", message.getMessageId());
|
|
|
+// ViewUtil.httpSendRequest(context, requestUrl, params, handler, null, PRAISE_WHAT, null, null, "get");
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 赞或者取消赞
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @param isPraise
|
|
|
+ */
|
|
|
+ private void praiseOrCancle(final int position, final boolean isPraise) {
|
|
|
+ pon = position;
|
|
|
+ final PublicMessage message = messages.get(position);
|
|
|
+ if (message == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ HashMap<String, String> params = new HashMap<String, String>();
|
|
|
+ params.put("access_token", MyApplication.getInstance().mAccessToken);
|
|
|
+ params.put("messageId", message.getMessageId());
|
|
|
+ String requestUrl = null;
|
|
|
+ if (isPraise) {
|
|
|
+ requestUrl = MyApplication.getInstance().getConfig().MSG_PRAISE_ADD;
|
|
|
+ } else {
|
|
|
+ requestUrl = MyApplication.getInstance().getConfig().MSG_PRAISE_DELETE;
|
|
|
+ }
|
|
|
+
|
|
|
+ StringJsonObjectRequest<Void> request = new StringJsonObjectRequest<Void>(requestUrl, new Response.ErrorListener() {
|
|
|
+ @Override
|
|
|
+ public void onErrorResponse(VolleyError arg0) {
|
|
|
+ ToastUtil.showErrorNet(context);
|
|
|
+ }
|
|
|
+ }, new StringJsonObjectRequest.Listener<Void>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResponse(ObjectResult<Void> result) {
|
|
|
+ boolean success = Result.defaultParser(context, result, true);
|
|
|
+ if (success) {
|
|
|
+ message.setIsPraise(isPraise ? 1 : 0);
|
|
|
+ List<Praise> praises = message.getPraises();
|
|
|
+ if (praises == null) {
|
|
|
+ praises = new ArrayList<Praise>();
|
|
|
+ message.setPraises(praises);
|
|
|
+ }
|
|
|
+ int praiseCount = message.getPraise();
|
|
|
+ if (isPraise) {// 代表我点赞
|
|
|
+ // 消息实体的改变
|
|
|
+ Praise praise = new Praise();
|
|
|
+ praise.setUserId(MyApplication.getInstance().mLoginUser.getUserId());
|
|
|
+ praise.setNickName(MyApplication.getInstance().mLoginUser.getNickName());
|
|
|
+ praises.add(0, praise);
|
|
|
+ praiseCount++;
|
|
|
+ message.setPraise(praiseCount);
|
|
|
+ } else {// 取消我的赞
|
|
|
+ // 消息实体的改变
|
|
|
+ for (int i = 0; i < praises.size(); i++) {
|
|
|
+ if (MyApplication.getInstance().mLoginUser.getUserId().equals(praises.get(i).getUserId())) {
|
|
|
+ praises.remove(i);
|
|
|
+ praiseCount--;
|
|
|
+ message.setPraise(praiseCount);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ notifyDataSetChanged();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, Void.class, params);
|
|
|
+ ((BaseActivity) context).addDefaultRequest(request);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|