10

在带有三个选项卡(主页、仪表板、通知)的底部导航中。每个底部导航选项卡都是一个片段。第一个标签,即。主页片段包含另一个顶部导航选项卡,该选项卡具有四个选项卡(选项卡 1、选项卡 2、选项卡 3、选项卡 4)。

在此处输入图像描述

问题

  1. 当直接从主页选项卡导航到通知选项卡并返回主页选项卡时,Tab1/以前选择的选项卡(顶部导航选项卡)不会加载选项卡的内容。

  2. 当从选项卡 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);
    }
}

问题

  1. 代码有什么问题?
  2. 如何解决这个问题?(布局是必需的)或者有没有更好的方法来从屏幕截图中提出布局?
4

2 回答 2

31

实际上,这是一个常见的错误——你在片段内部使用FragmentManager了你Activity的片段,但是由于这个片段包含另一个片段,你必须使用FragmentManager片段本身。所以修复很简单——你只需要改变你getActivity().getSupportFragmentManager()getChildFragmentManager()片段内部,所以代码将是:

private void setupViewPager(ViewPager viewPager) {
    TabViewPagerAdapter adapter = new TabViewPagerAdapter(getChildFragmentManager());
    ...
    ...
    viewPager.setAdapter(adapter);
}

这应该可以按预期工作。

于 2017-06-18T09:44:39.650 回答
-1

尝试getChildFragmentManager()使用getSupportFragmentManager().

于 2020-04-29T13:07:54.377 回答