如何在片段上添加带有片段的ViewPager?

时间:2018-07-06 07:42:53

标签: android android-fragments android-viewpager

我正在构建一个android应用程序,其中需要添加viewPager。 ViewPager将保留片段,并向左和向右滑动。

有人可以帮助我完成上述任务,但我无法为其找到教程。我在添加片段而不是活动的viewPager片段时遇到了很多问题

3 个答案:

答案 0 :(得分:0)

检查以下内容:https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

如果不需要,请删除TabLayout

答案 1 :(得分:0)

从Android 4.2开始,存在嵌套的片段。http://developer.android.com/about/versions/android-4.2.html#NestedFragments现在,支持库还包括对旧版Android的支持。

因此您可以执行以下操作:

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
    mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
    }

答案 2 :(得分:0)

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_lineup_team_b, container, false);
    unbinder = ButterKnife.bind(this, view);
    viewpager.setOffscreenPageLimit(3);                                     //5 is number of tabs

    tabsFrag = (TabLayout) view.findViewById(R.id.tabs_frag);

    toolbar.setVisibility(View.GONE);
    return view;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setupViewPager(viewpager);
    if(ViewCompat.isLaidOut(tabsFrag)) {
        setViewPagerListener();
    } else {
        tabsFrag.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                setViewPagerListener();
                tabsFrag.removeOnLayoutChangeListener(this);
            }
        });
    }
}

private void setViewPagerListener() {
    tabsFrag.setupWithViewPager(viewpager);
    // use class TabLayout.ViewPagerOnTabSelectedListener
    // note that it's a class not an interface as OnTabSelectedListener, so you can't implement it in your activity/fragment
    // methods are optional, so if you don't use them, you can not override them (e.g. onTabUnselected)
    tabsFrag.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewpager) {
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            super.onTabReselected(tab);
        }

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            super.onTabSelected(tab);
        }
    });
}

private void setupViewPager(ViewPager viewPager) {
    adapter = new ViewPagerAdapter(getChildFragmentManager());
    adapter.addFrag(new LineUpFragment_A(), "LineUp");
    adapter.addFrag(new SubstitutionFragment_A(), "Substitution");
    adapter.addFrag(new CoachFragment_A(), "Coach");

    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

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

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

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