在第一课
学前准备:掌握ViewPage第一课http://blog.csdn.net/wei_chong_chong/article/details/50468832
为ViewPage添加标题:
PageTabStrip是PageTitleStrip的子类
PageTabStrip的使用与PageTitleStrip完全一样,
实现效果,pageTabStrip的标题文字带有Indicator,并且可以通过点击标题切换页面的显示。
使用直接把下面的第一步中的<android.support.v4.view.PagerTitleStrip换成android.support.v4.view.PagerTabStrip就行了
布局文件:
在<android.support.v4.view.ViewPager节点下添加子节点:
<android.support.v4.view.PagerTabStrip android:layout_width="fill_parent" android:layout_height="50dp" > </android.support.v4.view.PagerTabStrip>
<android.support.v4.view.ViewPager android:id="@+id/vp_viewpage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/rg_tab" > <android.support.v4.view.PagerTabStrip android:layout_width="fill_parent" android:layout_height="50dp" > </android.support.v4.view.PagerTabStrip> </android.support.v4.view.ViewPager>
第二步:MainActivity中添加
private List<String> titles;具体如下:
public class MainActivity extends Activity { private ViewPager viewpager; private List<View> children; private List<String> titles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewpager = (ViewPager) findViewById(R.id.vp_viewpage); children = new ArrayList<View>(); children.add(LayoutInflater.from(this).inflate(R.layout.page01, null)); children.add(getLayoutInflater().inflate(R.layout.page02, null)); children.add(getLayoutInflater().inflate(R.layout.page03, null)); children.add(getLayoutInflater().inflate(R.layout.page04, null)); titles = new ArrayList<String>(); titles.add("page01"); titles.add("page02"); titles.add("page03"); titles.add("page04"); viewpager.setAdapter(new InnerPagerAdapter()); } public class InnerPagerAdapter extends PagerAdapter{ @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titles.get(position); } @Override public int getCount() { //获取子级布局的数量 return children.size();//返回viewpage容器中子级布局的个数, } @Override public boolean isViewFromObject(View view, Object object) { // 判断某个View对象是否为当前被添加到ViewPager容器中的对象 return view == object; } //下面两个方法是PageAdapter中必须实现的非抽象方法 @Override public Object instantiateItem(ViewGroup container, int position) { // 实例化ViewPager容器中指定的position位置需要显示的View对象 View view = children.get(position); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { //在ViewPager中移除指定的position位置的View对象 container.removeView(children.get(position)); } } }