CommentAdapter.java 3.09 KB
package com.cnlive.im.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cnlive.im.R;
import com.cnlive.libs.util.model.CommentItem;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈硕
 * @time 2017/6/9  10:56
 * @desc ${TODD}
 */
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentHolder> {

    protected List<CommentItem> mItems = new ArrayList<CommentItem>();
    protected OnImageViewClick onImageViewClick;
    Context context;
    private final LayoutInflater mLayoutInflater;
    private CommentHolder commentHolder;

    public CommentAdapter(Context context) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);

    }

    public void addItems(List<CommentItem> items) {
        mItems.addAll(items);
        notifyDataSetChanged();
    }

    public void refreshItems(List<CommentItem> items) {
        mItems.clear();
        mItems.addAll(items);
        notifyDataSetChanged();
    }

    public void clearItems() {
        mItems.clear();
    }


    @Override
    public CommentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.commet_item, parent, false);
        commentHolder = new CommentHolder(view);
        return commentHolder;
    }

    @Override
    public void onBindViewHolder(CommentHolder holder, int position) {

        final CommentItem commentItem = mItems.get(position);
        commentHolder.nick_name.setText(commentItem.getUid());
        commentHolder.comment_message.setText(commentItem.getContent());
        commentHolder.comment_good_count.setText(commentItem.getPraise());
        commentHolder.good_imv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onImageViewClick != null) {
                    onImageViewClick.praiseComment(commentItem);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public void setOnImageViewClick(OnImageViewClick onImageViewClick) {
        this.onImageViewClick = onImageViewClick;
    }

   public interface OnImageViewClick {
        void praiseComment(CommentItem commentItem);
    }

    class CommentHolder extends RecyclerView.ViewHolder {

        public final TextView nick_name;
        public final TextView comment_message;
        public final TextView comment_good_count;
        public final ImageView good_imv1;

        public CommentHolder(View itemView) {
            super(itemView);
            nick_name = (TextView) itemView.findViewById(R.id.nick_name);
            good_imv1 = (ImageView) itemView.findViewById(R.id.good_imv);
            comment_message = (TextView) itemView.findViewById(R.id.comment_message);
            comment_good_count = (TextView) itemView.findViewById(R.id.comment_good_count);


        }


    }


}