Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a viewpager inside of a fragment?

I have been looking and having a hard time finding a clear cut example? I am trying to understand how to create a viewpager within a fragment that is open as a drawer item from my mainActivity...

This is my attempt but I think I am doing something wrong whether it be that I am including it incorrectly or have it in the wrong spot...

public class RandomFragment extends android.app.Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_random, container, false);

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

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

    public static class MyAdapter extends FragmentPagerAdapter {

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

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

        @Override
        public Fragment getItem(int position) {
            Bundle args = new Bundle();
            args.putInt(TextViewFragment.POSITION_KEY, position);
            return TextViewFragment.newInstance(args);
        }
}
like image 385
Lion789 Avatar asked Dec 02 '25 07:12

Lion789


1 Answers

I use this one hope it help you too : https://github.com/thecodepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter

So you can use this one: https://github.com/astuetz/PagerSlidingTabStrip

like image 133
Smile2Life Avatar answered Dec 03 '25 21:12

Smile2Life



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!