问题描述
我正在学习使用 ViewPager 和 PagerTabStrip 来实现导航栏.我已经实现了,我的问题是:每次打开应用新鲜,标题不显示,但我刷一次后,标题又出现,然后一切正常.代码如下:
定制适配器
公共类 MyPagerAdapter 扩展 PagerAdapter {私人列表<查看>视图列表;私有列表<字符串>标题列表;public MyPagerAdapter(List<View>viewList, List<String>titleList){this.viewList = 视图列表;this.titleList = 标题列表;}@覆盖公共 int getCount() {返回 viewList.size();}@覆盖public boolean isViewFromObject(View view, Object o) {返回视图 == o;}@覆盖公共对象实例化项(ViewGroup 容器,int 位置){container.addView(viewList.get(position));返回 viewList.get(位置);}@覆盖public void destroyItem(ViewGroup container, int position, Object object) {container.removeView(viewList.get(position));}@覆盖公共 CharSequence getPageTitle(int position) {返回titleList.get(位置);}}
.xml 文件:
这是在我刷到第二页之后:
我真的很沮丧.谢谢!!
解决方案com.android.support:appcompat-v7:23.0.0出现问题.你可以参考这里 https://code.google.com/p/android/issues/detail?id=183127
在该链接中,谷歌支持团队提到该缺陷将在未来的版本中修复.所以现在的解决方案是使用 com.android.support:appcompat-v7:22.2.1 构建项目
更新:如果对您可行,那么您可以继续使用@nidheeshdas 提供的另一种解决方案.我尝试过简单的项目;它工作Activity的onResume()里面@nidheeshdas的修改解决方案
viewPager.setCurrentItem(1);viewPager.postDelayed(new Runnable() {@覆盖公共无效运行(){viewPager.setCurrentItem(0);}},100);新更新: 正如上面提到的谷歌问题跟踪链接和来自 JP Ventura 的评论.我已尝试使用新版本的库,问题似乎已解决.
I am learning to use ViewPager and PagerTabStrip to implement navigation bar. I have implemented it, my problem is: every time I open the app fresh, the titles don't show, but after I swipe it once, the titles all appear again, and then everything is normal. code shown below:
Customised Adapter
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 File:
<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>This is the screenshot of "Just clicked the app icon":
And this is after I swiped to the second page:
I'm really frustrated. Thanks!!
解决方案It is an issue appeared in com.android.support:appcompat-v7:23.0.0. You can refer here https://code.google.com/p/android/issues/detail?id=183127
In that link google support team have mention that defect would be fixed in future releases. So for now solution is build the project using com.android.support:appcompat-v7:22.2.1
Update : If feasible for you then you can go ahead with another solution provided by @nidheeshdas. I have tried on simple project; it work Modified solution of @nidheeshdas inside onResume() of Activity
viewPager.setCurrentItem(1); viewPager.postDelayed(new Runnable() { @Override public void run() { viewPager.setCurrentItem(0); } },100);New Update: As mentioned in the above google issue tracker link and comments from JP Ventura. I have tried with new version of library and issue seems to be fixed.