CourseXrecAdapter.java 3.8 KB
package com.hh.xuetubao.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
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.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.hh.xuetubao.R;
import com.hh.xuetubao.bean.FreeCourseBean;

import java.util.List;

public class CourseXrecAdapter extends RecyclerView.Adapter<CourseXrecAdapter.ViewHolder> {
    private List<FreeCourseBean.bean> mlist;
    private Context mcontext;

    public CourseXrecAdapter(List<FreeCourseBean.bean> mlist, Context mcontext) {
        this.mlist = mlist;
        this.mcontext = mcontext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mcontext).inflate(R.layout.xrv_home, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        if (mlist.get(position) != null) {
            if (mlist.get(position).getLessonName() != null)
                holder.title.setText(mlist.get(position).getLessonName());
            if (mlist.get(position).getDifficult() != null)
                holder.nanyi.setText(mlist.get(position).getDifficult());
            if (mlist.get(position).getLearningPeopleCount() != null)
                holder.count.setText(mlist.get(position).getLearningPeopleCount());
            if (mlist.get(position).getLessonCoverPic() != null) {
            }

            RoundedCorners roundedCorners = new RoundedCorners(10);//数字为圆角度数
            RequestOptions coverRequestOptions = new RequestOptions()
                    .transforms(new CenterCrop(), roundedCorners)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                    .skipMemoryCache(true);//不做内存缓存

            Glide.with(mcontext).load(mlist.get(position).getLessonCoverPic()).apply(coverRequestOptions).into(holder.img);

            if (position == mlist.size() - 1) {
                holder.line.setVisibility(View.GONE);
            } else {
                holder.line.setVisibility(View.VISIBLE);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (turn != null) {
                        turn.turnTo(mlist.get(position).getLessonOid());
                    }
                }
            });
        }
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView title;
        private final TextView nanyi;
        private final TextView count;
        private final ImageView img;
        private final View line;

        public ViewHolder(View itemView) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.title);
            nanyi = (TextView) itemView.findViewById(R.id.tv_nanyi);
            count = (TextView) itemView.findViewById(R.id.tv_count);
            img = (ImageView) itemView.findViewById(R.id.img);
            line = itemView.findViewById(R.id.line);
        }
    }

    private Turn turn;

    public void setTurn(Turn turn) {
        this.turn = turn;
    }

    public interface Turn {
        void turnTo(String lessonOid);
    }
}