片段

时间:2018-01-12 08:33:35

标签: android android-layout android-fragments android-tablayout android-nested-fragment

我按照这个https://www.youtube.com/watch?v=zcnT-3F-9JA创建了标签,但在本教程中,标签位于一个活动中,因为我需要它们在片段之类的嵌套片段。 对于片段,我在onCreateView中添加了代码,我在尝试创建TrainSearchFragmentAdapter的实例时遇到错误,这是另一个扩展FragmentPagerAdapter的类。

这是我的主要活动,即导航抽屉活动 Screenshot of Nav Drawer

这是点击导航选项Screenshot of the fragment with tabs

时打开的片段

这是我父代码片段的代码:

public class TrainSearchFragment
        extends Fragment implements
        TrainSearchTrainByNoFragment.OnFragmentInteractionListener,
        TrainSearchSeatAvailabilityFragment.OnFragmentInteractionListener,
        TrainSearchRouteFragment.OnFragmentInteractionListener {
    public TrainSearchFragment() {
    }
//...
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_train_search, container, false);

        TabLayout tabLayout = (TabLayout)view.findViewById(R.id.fragment_train_search_tablayout);
        tabLayout.addTab(tabLayout.newTab().setText("Train By No."));
        tabLayout.addTab(tabLayout.newTab().setText("Seat Availability"));
        tabLayout.addTab(tabLayout.newTab().setText("Route"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


        final ViewPager viewPager = (ViewPager)view.findViewById(R.id.fragment_train_search_viewpager);
        final TrainSearchFragmentAdapter trainSearchFragmentAdapter = new TrainSearchFragmentAdapter(getFragmentManager());
        viewPager.setAdapter(trainSearchFragmentAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

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

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        return view;
    }
//...
}

我还尝试在互联网上使用getChildFragmentManager()代替getFragmentManager()我在多个地方使用getSupportFragmentManager(),但我现在发现它已经贬值了。

这是我的适配器片段代码:

public class TrainSearchFragmentAdapter extends FragmentPagerAdapter {

    int mNoOfTabs;
    public TrainSearchFragmentAdapter(FragmentManager fm) {
        super(fm);
        this.mNoOfTabs = 3;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                TrainSearchTrainByNoFragment trainSearchTrainByNoFragment = new TrainSearchTrainByNoFragment();
                return trainSearchTrainByNoFragment;
            case 1:
                TrainSearchSeatAvailabilityFragment trainSearchSeatAvailabilityFragment = new TrainSearchSeatAvailabilityFragment();
                return trainSearchSeatAvailabilityFragment;
            case 3:
                TrainSearchRouteFragment trainSearchRouteFragment = new TrainSearchRouteFragment();
                return trainSearchRouteFragment;
            default:
                return null;
        }

    }
    @Override
    public int getCount() {
        return mNoOfTabs;
    }
}

0 个答案:

没有答案