https://zippy.gfycat.com/GratefulKeyChamois.webm
情况如上, PagerTabStrip 的 title 刚打开程序不显示,必须要拉动到下一个界面才会出现。
在网上找了个遍,都没发现原因,代码也是最基本的初始化,没做任何自定义,感觉要么是 Bug 、要么是手机问题。
这是代码:
MainActivity.java
package com.example.system.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
List<View> viewList = new ArrayList<>();
View view1 = View.inflate(this, R.layout.view1, null);
View view2 = View.inflate(this, R.layout.view2, null);
View view3 = View.inflate(this, R.layout.view3, null);
View view4 = View.inflate(this, R.layout.view4, null);
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);
List<String> titleList = new ArrayList<>();
titleList.add("第一个页面");
titleList.add("第二个页面");
titleList.add("第三个页面");
titleList.add("第四个页面");
MyPagerAdapter myPagerAdapter = new MyPagerAdapter(viewList, titleList);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(myPagerAdapter);
}
}
PagerAdapter 适配器
package com.example.system.myapplication;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
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 object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = viewList.get(position);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = viewList.get(position);
container.removeView(view);
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v4.view.PagerTabStrip
android:id="@+id/tab"
android:layout_gravity="top"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
</LinearLayout>
1
v2pg 2016-08-17 09:09:45 +08:00
加这句 viewPager.setCurrentItem(1) 试试。
|
2
Cabana 2016-08-17 09:23:07 +08:00
代码懒得看,看效果应该是没有初始化的原因
|
4
kitalphaj 2016-08-17 10:32:50 +08:00
@XhstormR 试试把 layout_width 和 layout_height (特别是 layout_width)改成 match_parent. 我感觉一开始宽度是 0.
|
6
nielev 2016-08-17 10:42:57 +08:00
跑了下没什么问题——红米 note3
|
7
leegoogle 2016-08-17 10:50:44 +08:00
应该是手机问题,我运行了你的代码 在魅蓝 note2 、小米 note 、红米 note3 都试了,没有遇到你这个情况。
|
8
XhstormR OP @leegoogle 能帮忙在虚拟机里帮我测下 apk 吗,若 apk 也没问题,那我就只能刷机了。
|
13
kitalphaj 2016-08-17 14:18:34 +08:00 1
@XhstormR 结果是这确实是个 Bug 。。。。看看这里 https://code.google.com/p/android/issues/detail?id=183127 。。。链接里面有解决方法,这个问题在 v24.1.0 里面才修复。。。
|
14
kitalphaj 2016-08-17 14:19:06 +08:00
v23.1.0 好像也可以
|
16
billyyuan 2016-08-17 15:48:43 +08:00
This issue is occurring only in appcompat-v7 and design libs, which has now got fixed in 23.1.0 revision of Android Support Library.
|