package com.yuqirong.viewpagerindicator.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.yuqirong.viewpagerindicator.R;
import com.yuqirong.viewpagerindicator.view.ViewPagerIndicator;

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

public class MainActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private ViewPagerIndicator mViewPagerIndicator;
    private List<String> mTitles = Arrays.asList("Tab1", "Tab2", "Tab3", "Tab4",
            "Tab5", "Tab6", "Tab7", "Tab8", "Tab9");
    private List<Fragment> mTabContents = new ArrayList<Fragment>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);
        mViewPagerIndicator = (ViewPagerIndicator) findViewById(R.id.mViewPagerIndicator);
        initData();
    }

    private void initData() {
        for (int i = 0; i < mTitles.size(); i++) {
            VpSimpleFragment fragment = VpSimpleFragment.newInstance(mTitles.get(i));
            mTabContents.add(fragment);
        }
        mViewPager.setAdapter(mAdapter);
        mViewPagerIndicator.setViewPager(mViewPager);
    }

    FragmentPagerAdapter mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public int getCount() {
            return mTabContents.size();
        }

        @Override
        public Fragment getItem(int position) {
            return mTabContents.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles.get(position);
        }
    };

}
