[英]Fragments inside ViewPager issue
I have 3 Fragments inside a ViewPager
. 我在
ViewPager
有3个片段 。 For each Fragment
I set a different NavigationBar
color (programmatically). 我为每个
Fragment
设置了不同的NavigationBar
颜色(以编程方式)。 This is the code. 这是代码。
@Override
public void init(Bundle arg0) {
// TODO Auto-generated method stub
addSlide(new Fragment1());
addSlide(new Fragment2());
addSlide(new Fragment3());
}
private class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(Color.parseColor(BLUE));
}
Log.e("test", "1");
return inflater.inflate(R.layout.layout_intro, container, false);
}
}
private class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(Color.parseColor(RED));
}
Log.e("test", "2");
return inflater.inflate(R.layout.layout_intro_2, container, false);
}
}
private class Fragment3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(Color.parseColor(GREEN));
}
Log.e("test", "3");
return inflater.inflate(R.layout.layout_intro_3, container, false);
}
}
}
In Logcat
I see wrong numbers. 在
Logcat
我看到了错误的数字。 For example i'm in the first Fragment
and I see GREEN NavigationBar
and "2". 例如,我在第一个
Fragment
,看到的是GREEN NavigationBar
和“ 2”。 Why? 为什么? How can i solve?
我该如何解决?
This happens because the ViewPager
loads already the next fragment, so if you are on fragment one, fragment 2 is already created, if you swipe to fragment 2, fragment 3 will be created. 发生这种情况是因为
ViewPager
已经加载了下一个片段,所以如果您在片段1上,则已经创建了片段2,如果您滑动到片段2,将创建片段3。 That's why swiping is smooth. 这就是为什么刷卡很顺畅。
Todo what you want you need to add onPageChangeListener to your ViewPager
like this: 要执行您想要的操作,您需要像这样将onPageChangeListener添加到您的
ViewPager
:
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
switch(position) {
case 1: if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(Color.parseColor(BLUE));
}
break;
case 2: if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(Color.parseColor(RED));
}
break;
case 3: if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(Color.parseColor(GREEN));
}
break;
}
}
@Override
public void onPageScrolled(int position, float offset, int offsetPixel) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
and so on. 等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.