我正在学习使用ViewPager和PagerTabStrip来实现导航栏。我已经实现了它,我的问题是:每次我打开应用程序时,标题都不会显示,但当我滑动一次后,所有标题都会再次出现,然后一切就正常了。 下面是代码:
自定义适配器
public class MyPagerAdapter extends PagerAdapter {
private List<View> viewList;
private List<String> titleList;
public MyPagerAdapter(List<View> viewList, List<String> titleList){
this.viewList = viewList;
this.titleList = titleList;
}
@Override
public int getCount() {
return viewList.size();
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
@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));
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
.xml文件:
<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.PagerTabStrip
android:id="@+id/tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
</android.support.v4.view.ViewPager>
这是"Just clicked the app icon"的屏幕截图:
我真的很沮丧。谢谢!!