How To Use A Viewpager In A Fragment
Solution 1:
Put this code in your MainActivity. The below code will load the Fragment in your Layout.
Step 1
Fragmentfragment=newPageSlider();
FragmentManagermanager= getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content, fragment).commit();
Next we need to implement the ViewPager
Step 2
publicclassPageSliderextendsFragment {
publicPageSlider() {
// Required empty public constructor
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.page_slider, container, false);
}
@OverridepublicvoidonViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onViewCreated(view, savedInstanceState);
PagerSlidingTabStrip tab = (PagerSlidingTabStrip) view
.findViewById(com.akshay.protocol10.asplayer.R.id.tabs);
ViewPager pager=(ViewPager)view .findViewById(com.akshay.protocol10.asplayer.R.id.view_pager);
MyPagerAdapter adapter = newMyPagerAdapter(getChildFragmentManager());
pager.setAdapter(adapter);
tab.setViewPager(pager);
}
// TODO: Rename method, update argument and hook method into UI eventpublicvoidonButtonPressed(Uri uri) {
}
@OverridepublicvoidonAttach(Activity activity) {
super.onAttach(activity);
}
@OverridepublicvoidonDetach() {
super.onDetach();
}
publicinterfaceOnFragmentInteractionListener {
// TODO: Update argument type and namepublicvoidonFragmentInteraction(Uri uri);
}
}
Step 3: Create the Adapter
publicclassMyPagerAdapterextendsFragmentPagerAdapter {
privatefinal String[] TITLES = { "Album", "Artist", "Songs", "Genre" };
publicMyPagerAdapter(FragmentManager childFragmentManager) {
// TODO Auto-generated constructor stubsuper(childFragmentManager);
}
@Overridepublic CharSequence getPageTitle(int position) {
// TODO Auto-generated method stubreturn TITLES[position];
}
@Overridepublic Fragment getItem(int index) {
// TODO Auto-generated method stubFragmentfragment=null;
if (index == 0) {
fragment = newAlbums();
} elseif (index == 1) {
fragment = newArtistFragment();
} elseif (index == 2) {
fragment = newTracksFragment();
} elseif (index == 3) {
fragment = newGenreFragment();
}
return fragment;
}
@OverridepublicintgetCount() {
// TODO Auto-generated method stubreturn TITLES.length;
}
}
Solution 2:
Android does not support fragments inside of fragments. Either switch your ViewPager
to use a PagerAdapter
that does not use fragments, or do not put the ViewPager
in a fragment.
EDIT:
Now, Android supports fragments inside fragments by using Fragment.getChildFragmentManager() . Have a look at this answer.
Solution 3:
Have you try to replace extends Fragment
by extends android.support.v4.app.Fragment
?
When you do this, getFragmentManager() refers to android.support.v4.app.FragmentManager.
Solution 4:
Check the fragment extends class it should be imported from android.support.v4.app.Fragment
Not android.app.Fragment
Post a Comment for "How To Use A Viewpager In A Fragment"