这里简单使用一下
PagerAdapter中至少要实现的四个方法
public Object instantiateItem(ViewGroup container, int position)
public void destroyItem(ViewGroup container, int position,Object object)
public int getCount()
public boolean isViewFromObject(View arg0, Object arg1)
1. 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/tab_bg">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/tab_bg">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
2. Activity
public class MainActivity extends Activity {
ViewPager pager = null;
PagerTabStrip tabStrip = null;
ArrayList<View> viewContainter = new ArrayList<View>();
ArrayList<String> titleContainer = new ArrayList<String>();
public String TAG = "tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) this.findViewById(R.id.viewpager);
tabStrip = (PagerTabStrip) this.findViewById(R.id.tabstrip);
//取消tab下面的长横线
// tabStrip.setDrawFullUnderline(false);
tabStrip.setBackgroundColor(Color.GRAY);
//设置当前tab页签的下划线颜色
tabStrip.setTabIndicatorColor(Color.RED);
tabStrip.setTextSpacing(200);
View view1 = LayoutInflater.from(this).inflate(R.layout.item1, null);
View view2 = LayoutInflater.from(this).inflate(R.layout.item2, null);
View view3 = LayoutInflater.from(this).inflate(R.layout.item3, null);
//标签块
viewContainter.add(view1);
viewContainter.add(view2);
viewContainter.add(view3);
//标签项
titleContainer.add("网易新闻");
titleContainer.add("网易体育");
titleContainer.add("网易财经");
pager.setCurrentItem(0);
pager.setAdapter(new MyPagerAdapter());
}
class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return titleContainer.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
//滑动切换的时候销毁当前的组件
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewContainter.get(position));
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
//设置块标题
@Override
public CharSequence getPageTitle(int position) {
return titleContainer.get(position);
}
//每次滑动的时候生成的组件
@Override
public Object instantiateItem(ViewGroup container, int position) {
return viewContainter.add(viewContainter.get(position));
}
}
}