问题
>当我直接从“主页”选项卡导航到“通知”选项卡并返回“主页”选项卡时,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(主页)
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); }}
问题
>我做错了什么?
>我该如何解决这个问题? (布局是一项要求)还是有更好的方法来从屏幕截图中提出布局?
private voID setupVIEwPager(VIEwPager vIEwPager) { TabVIEwPagerAdapter adapter = new TabVIEwPagerAdapter(getChildFragmentManager()); ... ... vIEwPager.setAdapter(adapter);}
这应该按预期工作.
总结以上是内存溢出为你收集整理的android – 底部导航片段内的Tab片段全部内容,希望文章能够帮你解决android – 底部导航片段内的Tab片段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)