ViewPager 问题中的片段
Fragments inside ViewPager issue
我在 ViewPager
中有 3 个 片段 。对于每个 Fragment
我设置了不同的 NavigationBar
颜色(以编程方式)。这是代码。
@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);
}
}
}
在 Logcat
中,我看到了错误的数字。例如,我在第一个 Fragment
中,我看到绿色 NavigationBar
和“2”。为什么?我该如何解决?
发生这种情况是因为 ViewPager
已经加载了下一个片段,因此如果您在片段 1 上,片段 2 已经创建,如果您滑动到片段 2,将创建片段 3。这就是滑动流畅的原因。
要做你想做的事,你需要将 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) {
}
});
等等。
我在 ViewPager
中有 3 个 片段 。对于每个 Fragment
我设置了不同的 NavigationBar
颜色(以编程方式)。这是代码。
@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);
}
}
}
在 Logcat
中,我看到了错误的数字。例如,我在第一个 Fragment
中,我看到绿色 NavigationBar
和“2”。为什么?我该如何解决?
发生这种情况是因为 ViewPager
已经加载了下一个片段,因此如果您在片段 1 上,片段 2 已经创建,如果您滑动到片段 2,将创建片段 3。这就是滑动流畅的原因。
要做你想做的事,你需要将 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) {
}
});
等等。