package com.tenlionsoft.baselib.core.widget.base;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.core.retrofit_net.BaseUrlApi;
import com.tenlionsoft.baselib.core.widget.photoview.widget.PhotoView;

import java.util.List;

/**
 * 作者 : Adam on 2018/10/17.
 * 邮箱 : itgaojian@163.com
 * 描述 : 图片显示
 */
public class ImageAdapter extends PagerAdapter {
    private Context mContext;
    private List mImgs;

    public ImageAdapter(Context ctx, List imgs) {
        this.mContext = ctx;
        this.mImgs = imgs;
    }

    @Override
    public int getCount() {
        return mImgs.size();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.item_pic, container, false);
        PhotoView photoView = view.findViewById(R.id.pv_pic);
        RequestOptions options = new RequestOptions().error(R.drawable.ic_img_load_err).placeholder(R.drawable.ic_img_loading);
        Glide.with(mContext)
                .load(BaseUrlApi.BASE_URL + mImgs.get(position))
                .apply(options)
                .into(photoView);
        container.addView(view);
        return view;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }


}
