带有ViewPager的TabLayout无法在片段Android中运行

时间:2015-12-23 04:10:53

标签: android android-fragments android-viewpager android-tablayout

嗨,我在片段中有一个TabLayout和ViewPager。但是我不能在tab之间滑动来到子片段(在Activity中正常工作但在片段中不起作用)。

以下是片段中Tablayout的代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View inflateView = inflater.inflate(R.layout.fragment_assign_beacon_to_event, container, false);

        //Viewpager and TabLayout
        viewPager = (ViewPager)inflateView.findViewById(R.id.viewPager);
        viewPager.setAdapter(new CustomAdapter(getFragmentManager(), getContext()));

        tabLayout = (TabLayout)inflateView.findViewById(R.id.tabLayout);


        // Inflate the layout for this fragment
        return inflateView;
    }

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    tabLayout.post(new Runnable() {
                @Override
                public void run() {
                    tabLayout.setupWithViewPager(viewPager);

            }
        });

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
        });

Any here is my Custom Adapter class

private class CustomAdapter extends FragmentPagerAdapter {

        private String fragments[] = {"Assign Event", "Manage Event"};

        public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
            super(supportFragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
//                    return new AssignEventToBeacon();
                    return new Test1();
                case 1:
                    return new ManageEventAssigned();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragments[position];
        }

    }

我怀疑这条线有问题:

viewPager.setAdapter(new CustomAdapter(getFragmentManager(), getContext()));

Anyhelp非常感谢,谢谢

2 个答案:

答案 0 :(得分:7)

我做了一个类似的项目,这是我的片段代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.outer_fragment, container, false);
        setHasOptionsMenu(true);

        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }

        Log.v("Layout","Tabs");
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
        tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
        tabLayout.setTabTextColors(Color.parseColor("#707070"), Color.parseColor("#FFFFFF"));
        assert viewPager != null;
        tabLayout.setupWithViewPager(viewPager);

        return view;
    }

private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(getChildFragmentManager());
        adapter.addFragment(new Tab1Fragment(), "PHOTOS");
        adapter.addFragment(new Tab2Fragment(), "HI-FIVES");
        viewPager.setAdapter(adapter);
    }


static class Adapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }

答案 1 :(得分:1)

您需要在Fragment中使用getChildFragmentManager(),而不是getFragmentManager()

来自getChildFragmentManager() documentation

  

返回一个私有的FragmentManager来放置和管理Fragments   在该片段的内部。

来自getFragmentManager() documentation

  

返回FragmentManager以与关联的片段进行交互   这个片段的活动。

简单地说,

  • getChildFragmentManager()用于子片段
  • 对活动中的片段使用getFragmentManager()

一个人可以看到@this answer以获取完整的代码。