Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Add and Remove Fragments From FragmentPagerAdapter

I have a FragmentPagerAdapter for a viewPager Which initially has only one Fragment in it. I want to dynamically add a new Fragment to the adapter when user swipes from right to left, and dynamically remove a Fragment when user swipes from left to right.I have tried to use this library https://github.com/commonsguy/cwac-pager but in that library we have an option to add and remove fragments on button clicks. I have tried to add a OnPageChangeListener to the viewpager but the callback methods ( onPageScrolled and onPageScrollStateChanged) are being called more than once which results in addition of more than one fragment to the FragmentPagerAdapter. So please shed some light on how to do this.

like image 521
dora Avatar asked Nov 19 '13 13:11

dora


People also ask

Can we add or remove fragments dynamically within an activity?

The system calls this method when Android needs the layout for the fragment. The Fragment class and Fragment Transaction class allow you to add, remove and replace fragments in the layout of your activity. Fragments can be dynamically modified by the transaction.

How to add ViewPager in Android?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .

How to create ViewPager adapter in Android?

Steps for implementing viewpager: Adding the ViewPager widget to the XML layout (usually the main_layout). Creating an Adapter by extending the FragmentPagerAdapter or FragmentStatePagerAdapter class.


2 Answers

@dora: i think in your case FragmentStatePagerAdapter will help you. I have mentioned its use below as per my understanding.I hope it will help you in taking decision.

There are two ways to implement ViewPager: • FragmentStatePagerAdapter • FragmentPagerAdapter

FragmentStatePagerAdapter class consumes less memory, because it destroys fragments, as soon as they are not visible to user, keeping only saved state of that fragment

FragmentPagerAdapter: when there are less number of fragments. But using AndroidFragmentPagerAdapter for large number of fragments would result choppy and laggy UX.

Number of page hold by a viewPager? The number of items that any ViewPager will keep hold of is set by the setOffscreenPageLimit() method. The default value for the offscreen page limit is 3. This means ViewPager will track the currently visible page, one to the left, and one to the right. The number of tracked pages is always centered around the currently visible page.

Please follow this link for code: http://www.truiton.com/2013/05/android-fragmentpageradapter-example/

like image 98
Kapil Avatar answered Oct 14 '22 07:10

Kapil


I know this post is old, but I struggled to figure this out so I'll answer it anyway.

You want to use FragmentStatePagerAdapter and override getItemPosition(). Create a list of stuff you want to pass down to the fragment, call notifyDataSetChanged(), and you're all set!

Here's the adapter:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        List<String> mKeyList = new ArrayList<>();

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

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(mKeyList.get(position));
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return "SCOUT " + (getCount() - position);
        }

        public void add(int position, String key) {
            mKeyList.add(position, key);
            notifyDataSetChanged();
        }
    }

And here's the fragment:

public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SCOUT_KEY = "scout_key";

        public static PlaceholderFragment newInstance(String key) {

            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putString(ARG_SCOUT_KEY, key);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.current_scout_fragment, container, false);

            //getArguments().getString(ARG_SCOUT_KEY));

            return rootView;
        }
    }
like image 6
SUPERCILEX Avatar answered Oct 14 '22 05:10

SUPERCILEX