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:
我正在学习如何使用ViewPager和PagerTabStrip来实现导航条。我已经实现了,我的问题是:每次打开应用程序时,标题都不显示,但在我刷过一次之后,标题再次出现,然后一切正常。代码如下所示:
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:
. 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>
This is the screenshot of "Just clicked the app icon":
这是“刚刚点击app图标”的截图:
And this is after I swiped to the second page:
这是我翻到第二页后
I'm really frustrated. Thanks!!
我真的很沮丧。谢谢! !
4 个解决方案
#1
12
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
支持:appcompat-v7:23.0.0。您可以在这里参考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
在该链接中,谷歌支持团队提到缺陷将在未来的版本中得到修复。目前的解决方案是使用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
更新:如果可行,那么可以使用@nidheeshdas提供的另一个解决方案。我尝试过简单的项目;它可以在活动的onResume()中修改@nidheeshdas的解决方案
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.
新的更新:如上面提到的谷歌问题跟踪链接和来自JP文图拉的评论。我尝试了新版本的图书馆,问题似乎是固定的。
#2
2
Instead of using android.support.v4.view.PagerTabStrip , use android.support.design.widget.TabLayout for displaying tabs for viewPager. It is included in Google Design Support Library.
而不是使用android.support.v4.view。PagerTabStrip使用android.support.design.widget.TabLayout来显示viewPager的选项卡。它包含在谷歌设计支持库中。
See this link for more information http://android-developers.blogspot.in/2015/05/android-design-support-library.html
有关更多信息,请参见此链接:http://android- developers.blogspot.in5//201505/android -design-support-library.html
Just few lines:
只是几行:
viewPager=(ViewPager)v.findViewById(R.id.viewPager);
ViewPagerAdapter adapter=new ViewPagerAdapter(this.getChildFragmentManager(),doctor);
adapter.setViewPagerFragmentListener(this);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager); //Sync Tabs with viewPager
tabLayout.setTabsFromPagerAdapter(adapter); //Setup tabs titles
And to change the titles use the following code in ViewPagerAdapter
要更改标题,请在ViewPagerAdapter中使用以下代码
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Title 1";
case 1:
return "Title 2";
case 2:
return "Title 3";
}
return super.getPageTitle(position);
}
#3
1
I also recently started to have this problem, and after a little bit of testing I think I found a bug in Android's latest support package update.
我最近也开始出现这个问题,在进行了一些测试之后,我认为我在Android最新的支持包更新中发现了一个bug。
The problem appears in to be in com.android.support:appcompat-v7:23.0.0.
问题出现在com.android.support:appcompat-v7:23.0.0。
Try changing the dependency back to com.android.support:appcompat-v7:22.2.1 (second latest update) and see if that works.
尝试将依赖项更改回com.android.support:appcompat-v7:22.2.1(第二次更新),看看是否有效。
Unfortunately, I have yet to find any solution to get it to work with the latest support package update.
不幸的是,我还没有找到任何解决方案来让它与最新的支持包更新一起工作。
#4
1
Try this. Its seems to be working for me.
试试这个。它似乎对我有用。
@Override
protected void onResume() {
super.onResume();
pager.setCurrentItem(1);
Task.delay(500).continueWith(new Continuation<Void, Object>() {
@Override
public Object then(Task<Void> task) throws Exception {
pager.setCurrentItem(0);
return null;
}
}, Task.UI_THREAD_EXECUTOR);
}
onResume set the pager to 1 and then back to 0. This makes the title appear the page loads the first time.
onResume将寻呼机设置为1,然后返回到0。这使得标题出现在页面第一次加载时。
#1
12
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
支持:appcompat-v7:23.0.0。您可以在这里参考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
在该链接中,谷歌支持团队提到缺陷将在未来的版本中得到修复。目前的解决方案是使用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
更新:如果可行,那么可以使用@nidheeshdas提供的另一个解决方案。我尝试过简单的项目;它可以在活动的onResume()中修改@nidheeshdas的解决方案
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.
新的更新:如上面提到的谷歌问题跟踪链接和来自JP文图拉的评论。我尝试了新版本的图书馆,问题似乎是固定的。
#2
2
Instead of using android.support.v4.view.PagerTabStrip , use android.support.design.widget.TabLayout for displaying tabs for viewPager. It is included in Google Design Support Library.
而不是使用android.support.v4.view。PagerTabStrip使用android.support.design.widget.TabLayout来显示viewPager的选项卡。它包含在谷歌设计支持库中。
See this link for more information http://android-developers.blogspot.in/2015/05/android-design-support-library.html
有关更多信息,请参见此链接:http://android- developers.blogspot.in5//201505/android -design-support-library.html
Just few lines:
只是几行:
viewPager=(ViewPager)v.findViewById(R.id.viewPager);
ViewPagerAdapter adapter=new ViewPagerAdapter(this.getChildFragmentManager(),doctor);
adapter.setViewPagerFragmentListener(this);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager); //Sync Tabs with viewPager
tabLayout.setTabsFromPagerAdapter(adapter); //Setup tabs titles
And to change the titles use the following code in ViewPagerAdapter
要更改标题,请在ViewPagerAdapter中使用以下代码
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Title 1";
case 1:
return "Title 2";
case 2:
return "Title 3";
}
return super.getPageTitle(position);
}
#3
1
I also recently started to have this problem, and after a little bit of testing I think I found a bug in Android's latest support package update.
我最近也开始出现这个问题,在进行了一些测试之后,我认为我在Android最新的支持包更新中发现了一个bug。
The problem appears in to be in com.android.support:appcompat-v7:23.0.0.
问题出现在com.android.support:appcompat-v7:23.0.0。
Try changing the dependency back to com.android.support:appcompat-v7:22.2.1 (second latest update) and see if that works.
尝试将依赖项更改回com.android.support:appcompat-v7:22.2.1(第二次更新),看看是否有效。
Unfortunately, I have yet to find any solution to get it to work with the latest support package update.
不幸的是,我还没有找到任何解决方案来让它与最新的支持包更新一起工作。
#4
1
Try this. Its seems to be working for me.
试试这个。它似乎对我有用。
@Override
protected void onResume() {
super.onResume();
pager.setCurrentItem(1);
Task.delay(500).continueWith(new Continuation<Void, Object>() {
@Override
public Object then(Task<Void> task) throws Exception {
pager.setCurrentItem(0);
return null;
}
}, Task.UI_THREAD_EXECUTOR);
}
onResume set the pager to 1 and then back to 0. This makes the title appear the page loads the first time.
onResume将寻呼机设置为1,然后返回到0。这使得标题出现在页面第一次加载时。