简体   繁体   中英

Android PagerAdapter for different fragments

Is it a good idea to use PagerAdapter or a more specific implementation FragmentPagerAdapter or FragmentStatePagerAdapter when there are more than one fragments that are being implemented. In documentation for FragmentPagerAdapter it says: "This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs"

Currently, I have a linear layout in my mainActivity with buttons (ImageView) in it to call replace method which changes fragments. I would like to be able to swipe between these fragments. For example in the Instagram app, there is an action bar at the bottom of screen. I ma trying to implement similar functionality but with swipe feature between fragments.

This is how I did by using SlidingTabLayout and SlidingTabStrip (just copy and paste them into your project)

PagerAdapter mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
    {
        @Override
        public Fragment getItem(int position)
        {
            // return the Fragment depending on the position
        }

        @Override
        public int getCount()
        {
            // return tab count
        }

        @Override
        public CharSequence getPageTitle(int pos)
        {
            // return page title depending on the position
        }

        @Override
        public int getItemPosition(Object object)
        {
            // Enable refreshing when called notifyDataSetChanged() on the adapter by adding this piece of code.
            return PagerAdapter.POSITION_NONE;
        }
    };

    ViewPager vp_Tabs = (ViewPager) parentView.findViewById(R.id.vp_tabsParent);
    // vp_Tabs.setOffscreenPageLimit(pageCount); // Prevents pages from getting destroyed and re-created.
    vp_Tabs.setAdapter(mPagerAdapter);

    SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) parentView.findViewById(R.id.slidingTabLayout);
    mSlidingTabLayout.setDistributeEvenly(true); // Makes tabs take equal space

    mSlidingTabLayout.setViewPager(vp_Tabs);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM