public MsgListAdapter(Context context, String targetId) {
   initData(context);
   this.mTargetId = targetId;
   this.mConv = JMessageClient.getSingleConversation(mTargetId);
   this.mMsgList = mConv.getMessagesFromNewest(0, mOffset);
   reverse(mMsgList);
   mStart = mOffset;
   UserInfo userInfo = (UserInfo) mConv.getTargetInfo();
   if (!TextUtils.isEmpty(userInfo.getAvatar())) {
     userInfo.getAvatarBitmap(
         new GetAvatarBitmapCallback() {
           @Override
           public void gotResult(int status, String desc, Bitmap bitmap) {
             if (status == 0) {
               notifyDataSetChanged();
             } else {
               HandleResponseCode.onHandle(mContext, status, false);
             }
           }
         });
   }
   checkSendingImgMsg();
 }
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    final Message msg = mMsgList.get(position);
    final UserInfo userInfo = msg.getFromUser();
    final ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = createViewByType(msg, position);
      switch (msg.getContentType()) {
        case text:
          holder.headIcon = (CircleImageView) convertView.findViewById(R.id.avatar_iv);
          holder.displayName = (TextView) convertView.findViewById(R.id.display_name_tv);
          holder.txtContent = (TextView) convertView.findViewById(R.id.msg_content);
          holder.sendingIv = (ImageView) convertView.findViewById(R.id.sending_iv);
          holder.resend = (ImageButton) convertView.findViewById(R.id.fail_resend_ib);
          holder.groupChange = (TextView) convertView.findViewById(R.id.group_content);
          break;
        case image:
          holder.headIcon = (CircleImageView) convertView.findViewById(R.id.avatar_iv);
          holder.displayName = (TextView) convertView.findViewById(R.id.display_name_tv);
          holder.picture = (ImageView) convertView.findViewById(R.id.picture_iv);
          holder.sendingIv = (ImageView) convertView.findViewById(R.id.sending_iv);
          holder.progressTv = (TextView) convertView.findViewById((R.id.progress_tv));
          holder.resend = (ImageButton) convertView.findViewById(R.id.fail_resend_ib);
          break;
        case voice:
          holder.headIcon = (CircleImageView) convertView.findViewById(R.id.avatar_iv);
          holder.displayName = (TextView) convertView.findViewById(R.id.display_name_tv);
          holder.txtContent = (TextView) convertView.findViewById(R.id.msg_content);
          holder.voice = ((ImageView) convertView.findViewById(R.id.voice_iv));
          holder.sendingIv = (ImageView) convertView.findViewById(R.id.sending_iv);
          holder.voiceLength = (TextView) convertView.findViewById(R.id.voice_length_tv);
          holder.readStatus = (ImageView) convertView.findViewById(R.id.read_status_iv);
          holder.resend = (ImageButton) convertView.findViewById(R.id.fail_resend_ib);
          break;
        case location:
          holder.headIcon = (CircleImageView) convertView.findViewById(R.id.avatar_iv);
          holder.displayName = (TextView) convertView.findViewById(R.id.display_name_tv);
          holder.txtContent = (TextView) convertView.findViewById(R.id.msg_content);
          holder.sendingIv = (ImageView) convertView.findViewById(R.id.sending_iv);
          holder.resend = (ImageButton) convertView.findViewById(R.id.fail_resend_ib);
          break;
        case eventNotification:
          holder.groupChange = (TextView) convertView.findViewById(R.id.group_content);
          break;
        default:
          holder.groupChange = (TextView) convertView.findViewById(R.id.group_content);
      }
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    // 显示时间
    TextView msgTime = (TextView) convertView.findViewById(R.id.send_time_txt);
    long nowDate = msg.getCreateTime();
    if (mOffset == 18) {
      if (position == 0 || position % 18 == 0) {
        TimeFormat timeFormat = new TimeFormat(mContext, nowDate);
        msgTime.setText(timeFormat.getDetailTime());
        msgTime.setVisibility(View.VISIBLE);
      } else {
        long lastDate = mMsgList.get(position - 1).getCreateTime();
        // 如果两条消息之间的间隔超过十分钟则显示时间
        if (nowDate - lastDate > 600000) {
          TimeFormat timeFormat = new TimeFormat(mContext, nowDate);
          msgTime.setText(timeFormat.getDetailTime());
          msgTime.setVisibility(View.VISIBLE);
        } else {
          msgTime.setVisibility(View.GONE);
        }
      }
    } else {
      if (position == 0 || position == mOffset || (position - mOffset) % 18 == 0) {
        TimeFormat timeFormat = new TimeFormat(mContext, nowDate);

        msgTime.setText(timeFormat.getDetailTime());
        msgTime.setVisibility(View.VISIBLE);
      } else {
        long lastDate = mMsgList.get(position - 1).getCreateTime();
        // 如果两条消息之间的间隔超过十分钟则显示时间
        if (nowDate - lastDate > 600000) {
          TimeFormat timeFormat = new TimeFormat(mContext, nowDate);
          msgTime.setText(timeFormat.getDetailTime());
          msgTime.setVisibility(View.VISIBLE);
        } else {
          msgTime.setVisibility(View.GONE);
        }
      }
    }

    // 显示头像
    if (holder.headIcon != null) {
      if (userInfo != null && !TextUtils.isEmpty(userInfo.getAvatar())) {
        userInfo.getAvatarBitmap(
            new GetAvatarBitmapCallback() {
              @Override
              public void gotResult(int status, String desc, Bitmap bitmap) {
                if (status == 0) {
                  holder.headIcon.setImageBitmap(bitmap);
                } else {
                  holder.headIcon.setImageResource(R.drawable.jmui_head_icon);
                  HandleResponseCode.onHandle(mContext, status, false);
                }
              }
            });
      } else {
        holder.headIcon.setImageResource(R.drawable.jmui_head_icon);
      }

      // TODO 点击头像事件

    }

    OnLongClickListener longClickListener =
        new OnLongClickListener() {
          @Override
          public boolean onLongClick(View arg0) {
            // 长按文本弹出菜单
            String name = userInfo.getUserName();
            OnClickListener listener =
                new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                    switch (v.getId()) {
                      case R.id.copy_msg_btn:
                        if (msg.getContentType() == ContentType.text) {
                          final String content = ((TextContent) msg.getContent()).getText();
                          if (Build.VERSION.SDK_INT > 11) {
                            ClipboardManager clipboard =
                                (ClipboardManager)
                                    mContext.getSystemService(mContext.CLIPBOARD_SERVICE);
                            ClipData clip = ClipData.newPlainText("Simple text", content);
                            clipboard.setPrimaryClip(clip);
                          } else {
                            ClipboardManager clipboard =
                                (ClipboardManager)
                                    mContext.getSystemService(mContext.CLIPBOARD_SERVICE);
                            clipboard.setText(content); // 设置Clipboard 的内容
                            if (clipboard.hasText()) {
                              clipboard.getText();
                            }
                          }

                          Toast.makeText(
                                  mContext,
                                  mContext.getString(R.string.copy_toast),
                                  Toast.LENGTH_SHORT)
                              .show();
                          mDialog.dismiss();
                        }
                        break;
                      case R.id.forward_msg_btn:
                        mDialog.dismiss();
                        break;
                      case R.id.delete_msg_btn:
                        mConv.deleteMessage(msg.getId());
                        mMsgList.remove(position);
                        notifyDataSetChanged();
                        mDialog.dismiss();
                        break;
                    }
                  }
                };
            boolean hide = msg.getContentType() == ContentType.voice;
            mDialog = DialogCreator.createLongPressMessageDialog(mContext, name, hide, listener);
            mDialog.show();
            mDialog
                .getWindow()
                .setLayout((int) (0.8 * mWidth), WindowManager.LayoutParams.WRAP_CONTENT);
            return true;
          }
        };

    switch (msg.getContentType()) {
      case text:
        handleTextMsg(msg, holder, longClickListener);
        break;
      case image:
        handleImgMsg(msg, holder, position);
        break;
      case voice:
        handleVoiceMsg(msg, holder, position, longClickListener);
        break;
      case eventNotification:
        handleGroupChangeMsg(msg, holder, msgTime);
        break;
      default:
        handleCustomMsg(msg, holder);
    }

    return convertView;
  }
Пример #3
0
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;
    if (convertView == null) {
      LayoutInflater inflater = LayoutInflater.from(mContext);
      convertView = inflater.inflate(R.layout.all_member_item, null);
      viewHolder =
          new ViewHolder(
              (CircleImageView) convertView.findViewById(R.id.icon_iv),
              (TextView) convertView.findViewById(R.id.name),
              (CheckBox) convertView.findViewById(R.id.check_box_cb));
      convertView.setTag(viewHolder);
    } else {
      viewHolder = (ViewHolder) convertView.getTag();
    }

    final UserInfo userInfo = mMemberList.get(position);
    if (mIsDeleteMode) {
      if (position > 0) {
        viewHolder.checkBox.setVisibility(View.VISIBLE);
        viewHolder.checkBox.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                if (viewHolder.checkBox.isChecked()) {
                  mSelectedList.add(userInfo.getUserName());
                  mSelectMap.put(position, true);
                  addAnimation(viewHolder.checkBox);
                } else {
                  mSelectedList.remove(userInfo.getUserName());
                  mSelectMap.delete(position);
                }
              }
            });
        viewHolder.checkBox.setChecked(mSelectMap.get(position));
      } else {
        viewHolder.checkBox.setVisibility(View.INVISIBLE);
      }

    } else {
      viewHolder.checkBox.setVisibility(View.GONE);
    }

    if (!TextUtils.isEmpty(userInfo.getAvatar())) {
      userInfo.getAvatarBitmap(
          new GetAvatarBitmapCallback() {
            @Override
            public void gotResult(int status, String desc, Bitmap bitmap) {
              if (status == 0) {
                viewHolder.icon.setImageBitmap(bitmap);
              } else {
                viewHolder.icon.setImageResource(R.drawable.head_icon);
                HandleResponseCode.onHandle(mContext, status, false);
              }
            }
          });
    } else {
      viewHolder.icon.setImageResource(R.drawable.head_icon);
    }
    String displayName = userInfo.getNickname();
    if (TextUtils.isEmpty(displayName)) {
      viewHolder.displayName.setText(userInfo.getUserName());
    } else {
      viewHolder.displayName.setText(displayName);
    }

    return convertView;
  }