<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" >
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
PagerTabStrip is an interactive indicator of the current, next, and previous pages of a ViewPager
. It is intended to be used as a child view of a ViewPager widget in your XML layout. Add it as a child of a ViewPager in your layout file and set its android:layout_gravity to TOP or BOTTOM to pin it to the top or bottom of the ViewPager. The title from each page is supplied by the method getPageTitle(int)
in the adapter supplied to the ViewPager.
For a non-interactive indicator, see PagerTitleStrip
.
private List<View> views;
private List<String> titles;
LayoutInflater layoutInflater = getLayoutInflater();
View view1 = layoutInflater.inflate(R.layout.view1, null);
View view2 = layoutInflater.inflate(R.layout.view2, null);
View view3 = layoutInflater.inflate(R.layout.view3, null);
views.add(view1);
views.add(view2);
views.add(view3);
titles = new ArrayList<String>();
titles.add("tab1");
titles.add("tab2");
titles.add("tab3");
Base class providing the adapter to populate pages inside of a ViewPager
. You will most likely want to use a more specific implementation of this, such as FragmentPagerAdapter
or FragmentStatePagerAdapter
.
When you implement a PagerAdapter, you must override the following methods at minimum:
instantiateItem(ViewGroup, int)
destroyItem(ViewGroup, int, Object)
getCount()
isViewFromObject(View, Object)
PagerAdapter pageAdapter = new PagerAdapter() {
@Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager)container).removeView(views.get(position));
}
@Override
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
((ViewPager)container).addView(views.get(position));
return views.get(position);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return views.size();
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titles.get(position);
}
};
viewPager.setAdapter(pageAdapter);
源码下载:http://download.csdn.net/detail/xdwyyan/8065775