Im using a FragmentPagerAdapter to manage 3 Fragments.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new Latest();
case 1:
return fragment = new MySets();
case 2:
return fragment = new MyPictures();
default:
break;
}
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
//SET TITLE OF TABS
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1_latest).toUpperCase(l);
case 1:
return getString(R.string.title_section2_mysets).toUpperCase(l);
case 2:
return getString(R.string.title_section3_allpictures).toUpperCase(l);
}
return null;
}
}
All Fragements extend Fragment. To handle Click Events and a Custom Layout Im using a Adapter which extends BaseAdapter.
My Goal is to replace Fragment 2 through another Fragment. Is it correct that I have to add all Fragments with FragmentTransaction to replace them later?
But how can i dynamically add the fragments to use FragmentTransaction? Is there a way to add them at the getItem(int position) or is that the wrong way to go?
Edit
If you need the Fragment as soon as the app is launched then the approach you used above in getItem() is okay, you don't need to do the transaction from there.
If you want to replace the Fragment later on simply use the getSupportFragmentManger and a transaction in the Fragment.
NewFragment newFrag = new NewFragment();
FragmentTransaction transaction = getActivity()
.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.your_fragment_container_id, newFrag);
transaction.addToBackStack(null);
transaction.commit();
Related
If I use a TabLayout with FragmentPagerAdapter, the overridden function getItem(int position) just return one fragment. In case of lager screen I would like to have 2 fragments to be returned.
How can I do it? should wrap these 2 fragments into one fragment and use that instead or is there any better solution?
FragmentPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: //Ingredients
return IngredientsFragment.newInstance(mRecipe);
case 1: // Details
{
// TODO Here instead of DetailFragment I want to return
// two fragments called DetailFragment and StepFragment.
return DetailFragment.newInstance(mRecipe);
}
default:
throw new RuntimeException(this.toString() + " Wrong fragment!");
}
}
And then in my Activity onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initializing, etc.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
I do not see you have much of a wiggle room here.
These two options are exlusive:
You are returning one Fragment with two nested fragments (Wrapped fragment)
You are returning two fragments each anchored to his own tab.
If you would like two fragments to appear on singular page of ViewPager, you have no choice but to wrap them.
Otherwise swiping would go between these two fragments, which is the same as if they are completely different, that is non-correlated.
OK I managed to solve the problem using a Fragment consist of two other fragments as children.
Just do not forget in this case the FragmentManager object should be populated with getChildFragmentManager() to work properly.
For more info look at my BakingApp project DetailStepWideScreenFragment.java
GitHub Repos
Fragment Wrapper:
public class DetailStepWideScreenFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detail_step_wide_screen, container, false);
// I added the fragments here. StepFragment can be replaced using
// replaceStepFragment function.
DetailFragment detailFragment = DetailFragment.newInstance(mRecipe);
StepFragment stepFragment = StepFragment.newInstance(mRecipe, mStepId);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.fl_detail_fragment_wide_screen, detailFragment);
transaction.add(R.id.fl_step_fragment_wide_screen, stepFragment);
transaction.commit();
return view;
}
public void replaceStepFragment(String stepId) {
mStepId = stepId;
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
StepFragment stepFragment = StepFragment.newInstance(mRecipe, mStepId);
transaction.replace(R.id.fl_step_fragment_wide_screen, stepFragment);
transaction.commit();
}
}
PagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private DetailStepWideScreenFragment currentFragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
currentFragment = null;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: //Ingredients
return IngredientsFragment.newInstance(mRecipe);
case 1: // Details
{
// Show base on screen size.
if (mIsLargeScreen) {
DetailStepWideScreenFragment detailStepWideScreenFragment = DetailStepWideScreenFragment.newInstance(mRecipe, "0");
currentFragment = detailStepWideScreenFragment;
return detailStepWideScreenFragment;
} else {
return DetailFragment.newInstance(mRecipe);
}
}
default:
throw new RuntimeException(this.toString() + " Wrong fragment!");
}
}
#Override
public int getCount() { return 2; }
public DetailStepWideScreenFragment getCurrentFragment() {
return currentFragment;
}
}
I am trying to understand how tabs are working but I am confused. I created a sample app with Android Studio wizard where there is one fragment.
How I can have in this one fragment but in different tabs different elements I only need to do something in onCreateView or in FragmentPagerAdapter?
The part of the adapter is:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#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(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "CONTACTS";
case 1:
return "DETAILS";
case 2:
return "GENERAL";
}
return null;
}
}
An example on how to achieve the task.
//sending different arguments from the view pager adapter
public Fragment getItem(int position){
Fragment fragment=new PlaceHolderFragment();
//a bundle is used to wrap the data you send to the fragment
Bundle bundle=new Bundle();
//change whats inside the bundle based on your page
switch(position){
case 0:{
bundle.putInt("dataForThisPosition",12);
break;
}case 1:{
bundle.putInt("dataForThisPosition",13);
break;
}
//this can continue
}
//attach the bundle to the fragment
fragment.setArguments(bundle);
return fragment;
}
//then in your fragment class
//receiving the data sent from pager adapter
public View onCreateView(/*parameters*/){
Bundle receivedBundle=getArguments();
int datForThisPosition=recievedBundle.getInt("dataForThisPosition",12/*default value*/);
//create your view based on the info received.
}
I'm currently trying to create a tabView where each tab opens a different layout. I'm using android.support.design.widget.TabLayout and I successfully linked it with pagerView using an adapter. Now inside myFragment class if I call onCreateView and inflate a layout, the layout (fragment_main) is displayed in all tabs.
public static class MyFragment extends Fragment {
public MyFragment() {
}
public static MyFragment newInstance(int pageNumber) {
MyFragment myFragment = new MyFragment();
return myFragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
return rootView;
}
}
I have 3 fragments connecting 3 tabs. Now how to set different layout for each fragment.
My adapter class is
class MyPagerAdapter extends FragmentStatePagerAdapter {
String[] tabs;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs=getResources().getStringArray(R.array.tabs);
}
#Override
public Fragment getItem(int position) {
NavigationActivity.MyFragment myFragment = NavigationActivity.MyFragment.newInstance(position);
return myFragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
You are passing same fragment for all the position in the below method, which is causing the problem,
#Override
public Fragment getItem(int position) {
NavigationActivity.MyFragment myFragment = NavigationActivity.MyFragment.newInstance(position);
return myFragment;
}
For solution you have to return different Fragments for different position or at least with different layout.
ArrayList< Fragment > tabFragments = new ArrayList<Fragment>();
fragments.add(new FragmentA());
fragments.add(new FragmentB());
fragments.add(new FragmentC());
#Override
public Fragment getItem(int position) {
return tabFragments.get(position);
}
Again,
I found the below mentioned blog as very useful. Each steps is described there. It is using TabListener. If the implementation is not certainly the same which you are looking for but you may get a good overview,
Android Fragment Tabs Example:
http://examples.javacodegeeks.com/android/core/app/fragment/android-fragment-tabs-example/
In your getItem() method write,
Fragment frgmt = null;
switch (position) {
case 0:
frgmt = new fragment1(Context);
break;
case 1:
frgmt = new fragment2(Context);
break;
case 2:
frgmt = new fragment3(Context);
break;
default:
break;
}
return frgmt;
Then create these fragment and their respective layouts.
please go to this i think this could help you out---- https://codeload.github.com/culmor30/android-fragments-example/zip/master
My app have android listfragment, 3 pages with 3 title i want replace page title to a icon how can i?
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new firstFragment();
break;
case 1:
fragment = new secondFragment();
break;
case 2:
fragment = new thirdFragment();
break;
}
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.fragment_one).toUpperCase();
case 1:
return getString(R.string.fragment_two).toUpperCase();
case 2:
return getString(R.string.fragment_three).toUpperCase();
}
return null;
}
}
I want each page have a icon not a text title. And also how can i aligh each icon in center
You can make custom ActionBar and set some Layout. When your fragment will start then you can set you icon there, update style or something else...
You can provide the tabs in action bar and you can set the Drawables for each tab u want.
i want to set different pages for ViewPager from class. for example:
i have 4 activity A + B + C and D.
A include my ViewPager.
B +C + D are different pages that must be shown in ViewPager.(each of them have different contex)
my question is that, how can i recognize these pages into ViewPager?
thanks
You can add but better way is that you will create a activity and your four activity should be child fragment of the same .
In short your all activity should be fragments jst load the fregment when you swipe viewpage indicater
sample code for adapter
//User adapter according condition
final FragmentPagerAdapter adapter = new FragmentChildPageAdapter(getChildFragmentManager());
//final FragmentPagerAdapter adapter = new FragmentChildPageAdapter(getActivity().getSupportFragmentManager()));
final ViewPager pager = (ViewPager) v.findViewById(R.id.pager);
pager.setAdapter(adapter);
final TabPageIndicator indicator = (TabPageIndicator) v.findViewById(R.id.indicator);
indicator.setViewPager(pager);
//Adapter
class FragmentChildPageAdapter extends FragmentPagerAdapter implements
IconPagerAdapter {
public FragmentChildPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
//first fregment
break;
case 1:
//Second fregment
break;
case 2:
//third fragment
break;
case 3:
//Forth fragment
break;
}
return fragment;
}
#SuppressLint("DefaultLocale")
/*#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}*/
#Override
public int getCount() {
//return CONTENT.length;
return CONTENT.size();
}
#Override
public int getIconResId(int position) {
return (Integer) Icon.get(position % CONTENT.size());
}
}