package com.cky.learnandroiddetails.ViewPagerAdapterExtract;

import android.database.DataSetObserver;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.jude.rollviewpager.RollPagerView;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * Created by cuikangyuan on 2017/7/28.
 */

public abstract class LoopPagerAdapterExtracted extends PagerAdapter{

    private RollPagerView mViewPager;

    private ArrayList<View> mViewList = new ArrayList<>();

    public LoopPagerAdapterExtracted(RollPagerView viewPager) {
        this.mViewPager = viewPager;
        initPosition();
    }

    @Override
    public int getCount() {
        if (getRealCount() <= 1) {
            return getRealCount();
        } else {
            return Integer.MAX_VALUE;
        }
    }

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        int realPosition = position % getRealCount();
        View itemView = findViewByPosition(container,realPosition);
        container.addView(itemView);
        return itemView;
    }

    private View findViewByPosition(ViewGroup container,int position){
        for (View view : mViewList) {
            if (((int)view.getTag()) == position && view.getParent() == null){
                return view;
            }
        }
        View view = getView(container,position);
        view.setTag(position);
        mViewList.add(view);
        return view;
    }

    @Override
    public void notifyDataSetChanged() {
        mViewList.clear();
        initPosition();
        super.notifyDataSetChanged();
    }

    private void initPosition(){
        if (getRealCount() > 0) {
            int half = Integer.MAX_VALUE/2;
            int start = half - half%getRealCount();
            setCurrent(start);
        }
    }

    private void setCurrent(int index){
        try {
            Field field = ViewPager.class.getDeclaredField("mCurItem");
            field.setAccessible(true);
            field.set(mViewPager.getViewPager(),index);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }


    public abstract View getView(ViewGroup container, int position);
    public abstract int getRealCount();
}

@@ -1440,24 +1440,29 @@
 mView;%0A    %7D
+%0A    
 %0A%0A    privat

test commit