package com.android.chmo.ui.adpater;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;

import com.android.chmo.ui.fragment.home.ShowImageFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/2.
 */

public class ShowImageAdapter extends FragmentPagerAdapter {
    private Context mContext;

    private List<String> imgList = new ArrayList<>();

    private ImgClickListener clickListener;


    public ShowImageAdapter(Context context, FragmentManager fm, List<String> list) {
        super(fm);
        mContext = context;
        imgList = list;
    }



    public void setImgClickListener(ImgClickListener listener) {
        clickListener = listener;
    }

    @Override
    public Fragment getItem(final int position) {
        String url = imgList.get(position);
        ShowImageFragment fragment = (ShowImageFragment) Fragment.instantiate(mContext, ShowImageFragment.class.getName());
        Bundle args = new Bundle();
        args.putString("url",url);
        fragment.setArguments(args);

        fragment.setClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != clickListener) {
                    clickListener.onClick(position);
                }
            }
        });

        return fragment;
    }

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

    @Override
    public long getItemId(int position) {
        int hashCode = imgList.get(position).hashCode();
        return hashCode;
    }


    public interface ImgClickListener {
        void onClick(int pis);
    }
}
