package com.qf.day18_viewpager_demo_05;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
//向ViewPager通过PagerTitleStrip的子类PagerTabStrip添加可和用户互动的标题,即点击标题可以切换不同的ViewPager页面
public class MainActivity extends Activity {
private ViewPager vp_01;
private List<ImageView> data;
private MyAdapter adapter;
private int[] images = {R.drawable.slide1,
R.drawable.slide2,
R.drawable.slide3};
private List<String> titile;//所有的标题
private PagerTabStrip tab_01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp_01 = (ViewPager) findViewById(R.id.vp_01);
tab_01 = (PagerTabStrip) findViewById(R.id.tap_01);
tab_01.setDrawFullUnderline(false);//设置是否取消下划线
tab_01.setBackgroundColor(Color.RED); //设置背景颜色
tab_01.setTabIndicatorColor(Color.YELLOW);//设置下划线的颜色
data =new ArrayList<ImageView>();
for(int i=0;i<images.length;i++){
ImageView iv = new ImageView(this);
iv.setImageResource(images[i]);
data.add(iv);
}
//构建标题数据
titile = new ArrayList<String>();
titile.add("开始喝茶");
titile.add("开始倒茶");
titile.add("喝茶完毕");
adapter = new MyAdapter();
vp_01.setAdapter(adapter);
}
public class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView(data.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(data.get(position));
return data.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titile.get(position);
}
}
}