android – 底部导航片段内的Tab片段

前端之家收集整理的这篇文章主要介绍了android – 底部导航片段内的Tab片段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带有三个选项卡的底部导航(主页,仪表板,通知).每个底部导航选项卡都是一个片段.第一个标签即.主片段包含另外的顶部导航标签,其具有四个标签(标签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(主页)

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);
    }
}

问题

>我做错了什么?
>我该如何解决这个问题? (布局是一项要求)还是有更好的方法来从屏幕截图中提出布局?

@H_502_26@

解决方法

实际上,这是一个常见的错误 – 你在片段中使用Activity的FragmentManager,但由于这个片段包含另一个子片段,你必须使用片段本身的FragmentManager.所以修复很简单 – 你只需要在你的片段中更改getActivity().getSupportFragmentManager()到getChildFragmentManager(),所以代码将是:
private void setupViewPager(ViewPager viewPager) {
    TabViewPagerAdapter adapter = new TabViewPagerAdapter(getChildFragmentManager());
    ...
    ...
    viewPager.setAdapter(adapter);
}

这应该按预期工作.

@H_502_26@ @H_502_26@

猜你在找的Android相关文章