HomeQuestionAdapter.java 2.7 KB
package com.hh.xuetubao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
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.QuestionBankLstBean;

import java.util.List;

public class HomeQuestionAdapter extends BaseAdapter {
    private List<QuestionBankLstBean.QuestionBankbean> list;
    private Context context;

    public HomeQuestionAdapter(List<QuestionBankLstBean.QuestionBankbean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (list.size() > 3) {
            return 3;
        } else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.lv_home_question, null);

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

        ImageView img = (ImageView) view.findViewById(R.id.img);
        TextView title = (TextView) view.findViewById(R.id.title);
        View line = view.findViewById(R.id.line);

        if (list.get(position).getCatalogName() != null)
            title.setText(list.get(position).getCatalogName());
        if (list.get(position).getConverUrl() != null)
            Glide.with(context).load(list.get(position).getConverUrl()).apply(coverRequestOptions).into(img);

        if (list.size() > 3) {
            if (position == 2) {
                line.setVisibility(View.GONE);
            } else {
                line.setVisibility(View.VISIBLE);
            }
        } else {
            if (position == list.size() - 1) {
                line.setVisibility(View.GONE);
            } else {
                line.setVisibility(View.VISIBLE);
            }

        }
        return view;
    }

}