RecommendTopicAdapter.java 3.8 KB
Newer Older
konghaorui committed
1 2 3
package com.yidianling.dynamic.adapter;

import android.content.Context;
YKai committed
4
import androidx.recyclerview.widget.RecyclerView;
konghaorui committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ydl.ydl_image.module.GlideApp;
import com.ydl.ydl_image.transform.CropSquareTransformation;
import com.yidianling.dynamic.R;
import com.yidianling.dynamic.model.RecommendTopic;
import com.ydl.ydlcommon.view.widgets.RoundImageView;

import java.util.List;

/**
 * 推荐话题适配器
 * Created by xiongyu on 2017/3/14.
 */

public class RecommendTopicAdapter extends RecyclerView.Adapter<RecommendTopicViewHolder> {
    protected Context mContext;
    protected List<RecommendTopic> mDatas;
    protected LayoutInflater mInflater;
    public interface OnItemClickLister {
        void onItemClick(View view, int position);
    }

    private OnItemClickLister mOnItemClickLister;

    public void setOnItemClickLister(OnItemClickLister lister) {
        this.mOnItemClickLister = lister;
    }

    public RecommendTopicAdapter(List<RecommendTopic> mDatas, Context mContext) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecommendTopicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
konghaorui committed
45
        View view= mInflater.inflate(R.layout.dynamic_item_recommend_topic,parent,false);
konghaorui committed
46 47 48 49 50 51 52 53 54 55 56 57
        RecommendTopicViewHolder viewHolder= new RecommendTopicViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecommendTopicViewHolder holder, int position) {
//        holder.item_recommend_topic_rel.getBackground().setAlpha(120);
        RecommendTopic recommendTopic=mDatas.get(position);
        String img_url=recommendTopic.getImg_bg_url();
        String topic_title=recommendTopic.getTopic_title();
        GlideApp.with(mContext)
                .load(img_url)
konghaorui committed
58
                .error(R.drawable.dynamic_default_img)
konghaorui committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
                .transform(new CropSquareTransformation(mContext))//裁剪正方形
                .override(270, 150)
                .centerCrop()
                .into(holder.topic_iv);
        holder.topic_title_tv.setText(topic_title);
        setUpItemEvent(holder);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }
    /**
     * 添加股据到指定位置
     * @param pos 数据添加的位置
     * @param recommendtopic 数据
     */
    public  void addData(int pos,RecommendTopic recommendtopic){
        mDatas.add(recommendtopic);
        notifyItemInserted(pos);
    }

    /**
     * 删除股票到指定位置
     * @param pos
     */
    public void deleteData(int pos){
        mDatas.remove(pos);
        notifyItemRemoved(pos);
    }

    /**
     * 给item添加监听
     * @param holder
     */
    protected  void setUpItemEvent(final RecommendTopicViewHolder holder){
        if(mOnItemClickLister!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition=holder.getLayoutPosition();
                    mOnItemClickLister.onItemClick(holder.itemView,layoutPosition);

                }
            });
        }
    }
}

class RecommendTopicViewHolder extends RecyclerView.ViewHolder{
    RoundImageView topic_iv;
    TextView topic_title_tv;
//    RelativeLayout item_recommend_topic_rel;
    public RecommendTopicViewHolder(View itemView) {
        super(itemView);
//        item_recommend_topic_rel=(RelativeLayout) itemView.findViewById(item_recommend_topic_rel);
        topic_iv=(RoundImageView)itemView.findViewById(R.id.item_recommend_topic_iv);
        topic_title_tv=(TextView)itemView.findViewById(R.id.item_recommend_topic_tv);
    }
}