我有一个带有三个标签的底部导航(主页,仪表板,通知)。每个底部导航选项卡都是一个片段。第一个标签即。主页片段包含另一个顶部导航选项卡,其中包含四个选项卡(选项卡1,选项卡2,选项卡3,选项卡4)。
问题
当我从主页标签直接导航到通知标签并返回主页标签时,Tab1 /之前曾标签选中的选项卡(顶部导航选项卡)未加载选项卡的内容。
当我将标签从标签1 (主页片段标签)一直滑到通知标签并向后滑动,到达标签4 标签的内容未加载,并且首次从标签4 轻刷到标签3 ,滑动不会转到标签3 。标签指示器只需移动一点,然后按第二次滑动即可按预期进入标签3 。
该应用包含大量代码,因此我只需将完整代码链接到Github。
这里有我的代码
供您快速参考MainActivity.java
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
NavigationView navigationView;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
viewPager.setCurrentItem(0);
return true;
case R.id.navigation_dashboard:
viewPager.setCurrentItem(1);
return true;
case R.id.navigation_notifications:
viewPager.setCurrentItem(2);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Hide the activity toolbar
getSupportActionBar().hide();
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
final BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position){
case 0:
navigation.setSelectedItemId(R.id.navigation_home);
break;
case 1:
navigation.setSelectedItemId(R.id.navigation_dashboard);
break;
case 2:
navigation.setSelectedItemId(R.id.navigation_notifications);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
BottomNavPagerAdapter adapter = new BottomNavPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstFragment());
adapter.addFragment(new SecondFragment());
adapter.addFragment(new ThirdFragment());
viewPager.setAdapter(adapter);
}
}
FirstFragment.java(Home)
public class FirstFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager firstViewPager;
public FirstFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
firstViewPager = (ViewPager) rootView.findViewById(R.id.viewpager_content);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(firstViewPager);
setupViewPager(firstViewPager);
return rootView;
}
private void setupViewPager(ViewPager viewPager) {
TabViewPagerAdapter adapter = new TabViewPagerAdapter(getActivity().getSupportFragmentManager());
adapter.addFragment(new Tab1Fragment(), "Tab 1");
adapter.addFragment(new Tab1Fragment(), "Tab 2");
adapter.addFragment(new Tab1Fragment(), "Tab 3");
adapter.addFragment(new Tab1Fragment(), "Tab 4");
viewPager.setAdapter(adapter);
}
}
问题
答案 0 :(得分:19)
实际上,这是一个常见错误 - 您在片段中使用了FragmentManager
Activity
,但由于此片段包含另一个子片段,因此使用片段本身的FragmentManager
。因此修复很简单 - 您只需将片段内的getActivity().getSupportFragmentManager()
更改为getChildFragmentManager()
,因此代码将为:
private void setupViewPager(ViewPager viewPager) {
TabViewPagerAdapter adapter = new TabViewPagerAdapter(getChildFragmentManager());
...
...
viewPager.setAdapter(adapter);
}
这应该按预期工作。
答案 1 :(得分:-1)
尝试使用 <ul>
<li> row1
<ul> <-- how do I create this element?
<li>value 1</li>
<li>value 2</li>
<li>value 3</li>
</ul>
</li>
<li> row2
<ul> <-- ...and this one?
<li>value 1</li>
<li>value 1</li>
<li>value 1</li>
</ul>
</li>
</ul>
的{{1}}实例。