@Override public void onClick(View view) { // 防止item错乱 int position = Integer.parseInt(view.getTag().toString()); switch (view.getId()) { case R.id.ll_praise: praise(position); break; case R.id.ll_share: share(position); break; case R.id.iv_avator: // 进入用户详情界面 post = list.get(position); Intent intent = new Intent(context, MyDetailActivity.class); User user = post.getAuthor(); String currentUserObjectId = BmobUserManager.getInstance(context).getCurrentUserObjectId(); if (user.getObjectId().equals(currentUserObjectId)) { intent.putExtra("from", "me"); } else { // 如果是好友。 if (BmobDB.create(context).getContactList().contains((BmobChatUser) user)) { intent.putExtra("from", "friend"); } else { intent.putExtra("from", "other"); } intent.putExtra("target_user", post.getAuthor()); } context.startActivity(intent); default: break; } }
@Override public void onBindViewHolder(final MyViewHolder viewHolder, int position) { post = list.get(position); iv_avator = (ImageView) viewHolder.getView(R.id.iv_avator); TextView tv_author = (TextView) viewHolder.getView(R.id.tv_author); TextView tv_time = (TextView) viewHolder.getView(R.id.tv_time); TextView tv_content = (TextView) viewHolder.getView(R.id.tv_content); tv_praise = (TextView) viewHolder.getView(R.id.tv_praise); tv_reply = (TextView) viewHolder.getView(R.id.tv_reply); btn_praise = (Button) viewHolder.getView(R.id.btn_praise); btn_share = (Button) viewHolder.getView(R.id.btn_share); btn_reply = (Button) viewHolder.getView(R.id.btn_reply); ll_praise = (LinearLayout) viewHolder.getView(R.id.ll_praise); ll_share = (LinearLayout) viewHolder.getView(R.id.ll_share); iv_sex = (ImageView) viewHolder.getView(R.id.iv_sex); if (post.getAuthor().getSex().equals("男")) { iv_sex.setImageResource(R.drawable.blue_male); } else { iv_sex.setImageResource(R.drawable.red_female); } tv_praise.setTag(position); tv_praise.setText("赞"); if (post.getPraiseCount() == 0) { tv_praise.setText("赞"); } else { tv_praise.setText(post.getPraiseCount() + ""); } tv_reply.setText("评论"); BmobQuery<Reply> query = new BmobQuery<Reply>(); query.addWhereEqualTo("post", post); query.count( context, Reply.class, new CountListener() { @Override public void onSuccess(int i) { if (i != 0) { tv_reply.setText(i + ""); } } @Override public void onFailure(int i, String s) {} }); iv_avator.setOnClickListener(this); iv_avator.setTag(position); ll_praise.setOnClickListener(this); ll_praise.setTag(position); ll_share.setOnClickListener(this); ll_share.setTag(position); if (post.getIsPraise()) { btn_praise.setBackgroundResource(R.drawable.praise_press); } else { btn_praise.setBackgroundResource(R.drawable.praise_normal); } String avatar = post.getAvator(); if (avatar != null && !avatar.equals("")) { ImageLoader.getInstance().displayImage(avatar, iv_avator, ImageLoadOptions.getOptions()); } else { if (post.getAuthor().getSex().equals("男")) { iv_avator.setImageResource(R.mipmap.male_default_icon); } else { iv_avator.setImageResource(R.mipmap.female_default_icon); } } // 设置时间 long time = TimeUtil.stringToLong(post.getCreatedAt(), "yyyy-MM-dd HH:mm:ss"); tv_time.setText(TimeUtil.getDescriptionTimeFromTimestamp(time)); // 设置用户名 tv_author.setText(post.getAuthor().getNick()); // 设置文字内容 SpannableString spannableString = FaceTextUtil.toSpannableString(context, post.getContent()); tv_content.setText(spannableString); tv_content.setVisibility(View.VISIBLE); final List<String> imagesUrl = post.getImages(); iv_content = (ImageView) viewHolder.getView(R.id.iv_content); if (imagesUrl != null && imagesUrl.size() > 0) { // 只取出第一张图片 String url = imagesUrl.get(0); iv_content.setVisibility(View.VISIBLE); ImageLoader.getInstance().displayImage(url, iv_content, ImageLoadOptions.getOptions()); } else { // layout_content.setVisibility(View.GONE); iv_content.setVisibility(View.GONE); } iv_content.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(context, ImageBrowserActivity.class); ArrayList<String> photos = new ArrayList<String>(); photos.add(imagesUrl.get(0)); intent.putStringArrayListExtra("photos", photos); intent.putExtra("position", 0); context.startActivity(intent); } }); // 如果设置了回调,则设置点击事件 if (mOnItemClickLitener != null) { view.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { int pos = viewHolder.getLayoutPosition(); mOnItemClickLitener.onItemClick(view, pos); } }); view.setOnLongClickListener( new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { int pos = viewHolder.getLayoutPosition(); mOnItemClickLitener.onItemLongClick(view, pos); return false; } }); } }