Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why it is not possible to use ViewPager within a Fragment? It actually is

There are information that it is impossible to use ViewPager within a Fragment in many sources like "The Busy Coders Guide for Android Developers" by Mark Murphy, or posts like this on SO. I'm confused because I don't have such a problem and I successfully use ViewPager within my Fragment. The only distinction is that I instantiate a ViewPager not in onCreateView() method but in onActivityCreated(). And everything works perfectly fine.

So the question is - may be I just don't know something and this is not recommended for some reason to make UI instantiations in onActivityCreated()? But again - everything works just fine.

Here is the listing of the class and xml:

Class:

public class ViewPagerFragment extends Fragment {      static final int NUM_ITEMS = 2;      private ViewPagerAdapter mAdapter;     private ViewPager mPager;      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         return inflater.inflate(R.layout.view_pager_fragment, container, false);     }      @Override     public void onActivityCreated(Bundle savedInstanceState) {         super.onActivityCreated(savedInstanceState);          mAdapter = new ViewPagerAdapter(getFragmentManager());          mPager = (ViewPager) getView().findViewById(R.id.pager);         mPager.setAdapter(mAdapter);     }      public static class ViewPagerAdapter extends FragmentPagerAdapter {         public ViewPagerAdapter(FragmentManager fm) {             super(fm);         }          @Override         public Fragment getItem(int num) {             if (num == 0) {                 return new ItemsListFragment();             } else {                 return new FavsListFragment();             }         }          @Override         public int getCount() {             return NUM_ITEMS;         }     } } 

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               android:layout_width="fill_parent"               android:layout_height="fill_parent"               android:orientation="vertical">      <android.support.v4.view.ViewPager         android:id="@+id/pager"         android:layout_width="fill_parent"         android:layout_height="0dp"         android:layout_weight="1"/>  </LinearLayout> 
like image 441
Eugene Avatar asked Aug 23 '12 09:08

Eugene


People also ask

When should I use ViewPager?

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android.

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

Is ViewPager deprecated?

This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated.


1 Answers

To implement the View pager within a fragment use getChildFragmentManager() instead of getFragmentManager(). You can call setAdapter() for the ViewPager from onCreateView() or onViewCreated(), that is not a matter.

I hope this will help you guys.

like image 143
Rajkumar Nagarajan Avatar answered Sep 20 '22 03:09

Rajkumar Nagarajan