package com.navinfo.outdoor.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.navinfo.outdoor.R;

import java.util.ArrayList;

/**
 * 图片浏览的PagerAdapter
 */
public class ImagePagerAdapter extends PagerAdapter {
    Context context;
    ArrayList<String> imgsUrl;
    LayoutInflater inflater = null;

    ImageView full_image;
    public ImagePagerAdapter(Context context, ArrayList<String> imgsUrl) {
        this.context = context;
        this.imgsUrl = imgsUrl;
        inflater = LayoutInflater.from(context);

    }

    /** 动态加载数据 */
    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
    }

    @Override
    public int getCount() {
        return imgsUrl == null ? 0 : imgsUrl.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public int getItemPosition(Object object) {
        // TODO Auto-generated method stub
        return super.getItemPosition(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = inflater.from(context).inflate(R.layout.details_imageshow_item, null);
        full_image = (ImageView)view.findViewById(R.id.full_image);
        Glide.with(context).load(imgsUrl.get(position)).into(full_image);

        ((ViewPager) container).addView(view);
        return view;
    }

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