recent

2016-04-29 0 条评论 202 次阅读 0 人点赞

最做了一个聊天列表,类似QQ的聊天样式,来消息头像右上角提示未读消息数量想了好久,在adapter中用的badgeview,onBandViewHolder()要复用好几次.而badgeview又只有一个对象不能分配给每个讨论组的头像. 思路如下,声明一个BadgeView的数组.然后实例化数据源.size()次,看看结果吧.代码如下:

package com.crowdfundproducer.producer.adapter;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.crowdfundproducer.producer.R;
import com.crowdfundproducer.producer.entity.GroupEntity;
import com.crowdfundproducer.producer.view.BadgeView;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import org.xutils.common.Callback;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.util.List;
/**
* Created by TestSmirk on 2016/1/25.
   */
  public class TalkRecyclerViewAdapter extends RecyclerView.Adapter<talkRecyclerViewAdapter.ViewHolder> {
   private static final String TAG = "TalkRecyclerViewAdapter";
   private List<groupEntity.GroupListEntity> list;
   private OnItemClickListener onItemClickListener;
   private BadgeView[] badgeViews;
   private int onceBadgeView;
   public TalkRecyclerViewAdapter(List<groupEntity.GroupListEntity> list) {
       this.list = list;
       for (int i = 0; i < list.size(); i++) {
           badgeViews = new BadgeView[i];
       }
   }
   @Override
   public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View inflate = LayoutInflater.from(x.app()).inflate(R.layout.layout_talk, parent, false);
       ViewHolder viewHolder = new ViewHolder(inflate);
       x.view().inject(viewHolder, inflate);
       return viewHolder;
   }
   @Override
   public void onBindViewHolder(final ViewHolder holder, final int position) {
       if (list == null) return;
       onceBadgeView = 1;
       GroupEntity.GroupListEntity groupListEntity = list.get(position);
       EMConversation conversation = EMClient.getInstance().chatManager().getConversation(list.get(position).getGroup_id());
       if (conversation != null) {
           Log.d(TAG, "onBindViewHolder: " + conversation.getUnreadMsgCount());
           ImageOptions.Builder builder = new ImageOptions.Builder();
           /**
            *
            * 设置讨论组头像
            */
           if (conversation.getUnreadMsgCount() == 0) {
               badgeViews[position].setVisibility(View.GONE);
               badgeViews[position].hide();
               Log.d(TAG, "onBindViewHolder: GONE");
               Log.d(TAG, "onBindViewHolder: GONE1");
            } else {
                badgeViews[position].increment(conversation.getUnreadMsgCount());
                badgeViews[position].show();
            }
            builder.setLoadingDrawableId(R.mipmap.default_user);
            x.image().loadDrawable(groupListEntity.getMovie_pic(), builder.build(), new Callback.CommonCallback<drawable>() {
                @Override
                public void onSuccess(Drawable result) {
                    holder.iv_talk_pic.setImageDrawable(result);
                }
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                }
                @Override
                public void onCancelled(CancelledException cex) {
                }
                @Override
                public void onFinished() {
                }
            });
        }
        /**
         * 讨论组名称
         */
        holder.tv_talk_name.setText(groupListEntity.getProject_name());
        /**
         * 讨论组描述
         */
        EMConversation emMessages = EMClient.getInstance().chatManager().getConversation(groupListEntity.getGroup_id());
        if (emMessages != null) {
            if (emMessages.getLastMessage().getType() == EMMessage.Type.TXT) {
                holder.tv_talk_des.setText(emMessages.getLastMessage().getBody().toString().replaceAll("\"", "").replace("txt:", ""));
            } else {
                holder.tv_talk_des.setText("[不支持消息]");
            }
        }
        /**
         * 讨论组总人数
         */
        holder.tv_talk_sum.setText(x.app().getString(R.string.tv_talk_sum, groupListEntity.getUser_count() - 1));
        /**
         *
         * 讨论组点击事件
         */
        holder.layout_chatgroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.layout_chatgroup.setTag("holder.layout_chatgroup");
                onItemClickListener.OnItemClickListener(holder.layout_chatgroup, position);
            }
        });
    }
    @Override
    public int getItemCount() {
        return list == null ? 1 : list.size();
    }
    public void setOnClickListener(OnItemClickListener onClickListener) {
        this.onItemClickListener = onClickListener;
    }
    public interface OnItemClickListener {
        void OnItemClickListener(View v, int position);
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        @ViewInject(R.id.iv_talk_pic)
        private ImageView iv_talk_pic;
        @ViewInject(R.id.tv_talk_des)
        private TextView tv_talk_des;
        @ViewInject(R.id.tv_talk_name)
        private TextView tv_talk_name;
        @ViewInject(R.id.tv_talk_sum)
        private TextView tv_talk_sum;
        @ViewInject(R.id.layout_chatgroup)
        private RelativeLayout layout_chatgroup;
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}

TestSmirk

这个人太懒什么东西都没留下

文章评论(0)