Example #1
0
  @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;
            }
          });
    }
  }
Example #2
0
  /**
   * 点赞功能
   *
   * @param position 点击控件所对应item的位置
   */
  public void praise(final int position) {
    post = list.get(position);
    // 查询喜欢这个帖子的所有用户,因此查询的是用户表
    queryAllPrasieUsers();
    // 先得到帖子的点赞数
    int count = post.getPraiseCount();
    // Log.i(TAG,"currentUser="******"praiseCount");
      tv_praise.setText("" + (count + 1));
      //            if(!praiseUsers.contains(currentUser)){
      //               //判断当前user是否赞过
      //                Log.i(TAG, "当前用户有赞过,取消赞");
      //                //赞过,要取消赞
      //                post.increment("praiseCount", -1);
      //                BmobRelation relation=new BmobRelation();
      //                relation.remove(currentUser);
      //                post.setLikes(relation);
      //                //取消赞之后判断赞的数量
      //                if(post.getPraiseCount()==0){
      //                    btn_praise.setBackgroundResource(R.drawable.praise_normal);
      //                    tv_praise.setText("赞");
      //                    post.setIsPraise(false);
      //                }else{
      //                    btn_praise.setBackgroundResource(R.drawable.praise_press);
      //                    tv_praise.setText("" + post.getPraiseCount());
      //                }
      //            }else{
      //                //当前用户没有赞过,点赞
      //                Log.i(TAG, "当前用户没有赞过,点赞");
      //                btn_praise.setBackgroundResource(R.drawable.praise_press);
      //                post.increment("praiseCount");
      //                tv_praise.setText("" + post.getPraiseCount());
      //                BmobRelation relation=new BmobRelation();
      //                //将点赞的人添加到集合中
      //                relation.add(currentUser);
      //                post.setLikes(relation);
      //            }
    } else {
      Log.i(TAG, "无赞,点赞");
      post.increment("praiseCount");
      tv_praise.setText("" + (count + 1));
      btn_praise.setBackgroundResource(R.drawable.praise_press);
      post.setIsPraise(true);
      //            BmobRelation relation=new BmobRelation();
      //            //将点赞的人添加到集合中
      //            relation.add(currentUser);
      //            post.setLikes(relation);
    }

    post.update(
        context,
        new UpdateListener() {
          @Override
          public void onFailure(int arg0, String arg1) {}

          @Override
          public void onSuccess() {
            // 查找出更新后的内容,否则界面不会改变
            BmobQuery<Post> query = new BmobQuery<Post>();
            query.addWhereEqualTo("objectId", post.getObjectId());
            query.addQueryKeys("praiseCount");
            query.findObjects(
                context,
                new FindListener<Post>() {
                  @Override
                  public void onSuccess(List<Post> list) {
                    post.setPraiseCount(list.get(0).getPraiseCount());
                    notifyItemChanged(position, null);
                  }

                  @Override
                  public void onError(int i, String s) {}
                });
          }
        });
  }