将layout布局转换为view对象。
1>layoutInflater lf=getLayoutInflater().from(this);
lf.inflate(resource,root);
2>View.inflate(context,resource,root);
配置adapter
1>pageradapter 数据源:list<View>
2>FragmentPagerAdapter 数据源:list<Fragment>
3>FragmentStatePagerAdapter 数据源:list<Fragment>
android.support.v4.view.ViewPager
android.support.v4.view.pagerTabStrip
android.support.v4.view.pagerTitleStrip
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pager"
>
<android.support.v4.view.pagerTabStrip
android:layout_width= "wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tab"
>
</android.support.v4.view.pagerTabStrip>
<android.support.v4.view.pagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
>
</android.support.v4.view.pagerTitleStrip>
</android.support.v4.view.ViewPager >
public class MyPagerAdapet extends PagerAdapter{
private List<View> viewlist ;
private List<String> tablist ;
public MyPagerAdapet(List<View> viewlistList ,List<String> tablist){
this.viewlist =viewlistList ;
this.tablist =tablist;
}
/**
* 返回页卡的数量
*/
@Override
public int getCount() {
return viewlist .size();
}
/**
*判断当前的view是否来自于对象 arg0==arg1
*/
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
/**
* 实例化一个页卡
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView( viewlist.get(position));
return viewlist .get(position);
}
/**
* 销毁一个页卡
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView( viewlist.get(position));
}
/**
* 设置 viewpage页卡的标题
*/
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return tablist .get(position);
}
}
public class MainActivity extends ActionBarActivity {
private List<View> viewlist ;
private ViewPager pager ;
private PagerTabStrip tab;
private List<String> tablist ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
viewlist= new ArrayList<View>();
/**
* 通过view对象作为view怕给的数据源
*/
View view1=View. inflate(this, R.layout. view1, null );
viewlist.add(view1);
//初始化pager
pager=(ViewPager) findViewById(R.id. pager);
//创建pagerAdapter适配器
MyPagerAdapet adapet= new MyPagerAdapet(viewlist ,tablist );
//viewpager 加载适配器
pager.setAdapter(adapet);
}
}
FragmentPagerAdapter
使用fragment作为viewpager的数据源。