XML代码
<android.support.v4.view.ViewPager ViewPager控件
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MyViewPager"
android:layout_gravity="center"
>
<android.support.v4.view.PagerTitleStrip ViewPager的标题
android:id="@+id/pagerTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
>
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
Java 代码
需要一个ViewPager的适配器
实现一个继承自PagerAdapter的类
实现某些需要重载的方法
例如:
public class myAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO 自动生成的方法存根
return 0;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO 自动生成的方法存根
return false;
}
}
private ViewPager myViewPager;
private myAdapter myPagerAdapter;
private PagerTitleStrip myPagerTitle;
private LayoutInflater infater;
private List<View> views;
private List<String> titles;
myViewPager = (ViewPager) findViewById(R.id.MyViewPager);
myPagerTitle = (PagerTitleStrip) findViewById(R.id.pagerTitle);
infater = LayoutInflater.from(MainActivity.this);
View tab = infater.inflate(R.layout.card_recharge_detail, null);
views = new ArrayList<View>();
views.add(tab);
titles = new ArrayList<String>();
titles.add("第一页");
myPagerAdapter = new myAdapter();
myViewPager.setAdapter(myPagerAdapter);