I have the following activity wich show different tabs.
public class BagContent extends FragmentActivity implements ActionBar.TabListener, CosmeListener {
String[] profileTypes;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_bag_content);
mViewPager = (ViewPager) findViewById(R.id.pager);
final ActionBar actionBar = getActionBar();
actionBar.setTitle(getString(R.string.title_activity_bag_content) + " [" + bagName + "]");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
}
#Override
protected void onResume() {
super.onResume();
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FormatShortFragment();
case 1:
return new FormatLongFragment();
case 2:
return new FormatPlantFragment();
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_bag_content_format_short).toUpperCase(l);
case 1:
return getString(R.string.title_bag_content_format_long).toUpperCase(l);
case 2:
return getString(R.string.title_bag_content_format_plant).toUpperCase(l);
}
return null;
}
}
I have and array wiht different profile types, depending on each type i have to show some tabs or others.
Actually all tabs are been showing for every profile type. I tried to insert manually the tabs on the actionbar, but then the tabs are not showing the fragments correctly. Other problem is that navigation breaks when i go to an inexistent tab on the right edge.
I need help with this, how can I select just 1 tab for a specific profile type?
for example, how can I show just the title_bag_content_format_long and his fragment for profileTypes[1]?
Any walk aroun can be good, for example remove some tabs from some profile types or any other thing.
Thanks a lot guys, I really need help with this.
EDITED
I have made some modifications:
Added a profile type parameter to SectionsPagerAdapter constructor:
private String profileType;
public SectionsPagerAdapter(FragmentManager fm, String profileType) {
super(fm);
this.profileType = profileType;
}
Then i have modified this other methods to control the profile type:
#Override
public Fragment getItem(int position) {
if (this.profileType.equals(profileTypes[1])){
switch (position) {
case 0:
return new FormatPlantFragment();
}
} else {
switch (position) {
case 0:
return new FormatShortFragment();
case 1:
return new FormatLongFragment();
case 2:
return new FormatPlantFragment();
}
}
return null;
}
#Override
public int getCount() {
if (this.profileType.equals(profileTypes[1])){
return 1;
}
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
if (this.profileType.equals(profileTypes[1])){
switch (position) {
case 0:
return getString(R.string.title_bag_content_format_plant).toUpperCase(l);
}
} else {
switch (position) {
case 0:
return getString(R.string.title_bag_content_format_short).toUpperCase(l);
case 1:
return getString(R.string.title_bag_content_format_long).toUpperCase(l);
case 2:
return getString(R.string.title_bag_content_format_plant).toUpperCase(l);
}
}
return null;
}
This is working correctly and do the filter by profile type more versatile.
Thanks to Filpe.costa01 that gave me the push that I needed.
You're returning all the tabs because your method
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
return 3 tabs. Maybe you could use the SectionsPagerAdapter constructor to include the number of tabs you'd like, according to each profile wanted.
Something like this:
private mNumOfTabs;
public SectionsPagerAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
mNumOfTabs = numberOfTabs;
}
and you should replace your getCount() method for this one:
#Override
public int getCount() {
// Show 3 total pages.
return mNumOfTabs;
}
Organizing your profiles in an ascendant way (low profile = small number of tabs), you can use this method like it's now
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FormatShortFragment();
case 1:
return new FormatLongFragment();
case 2:
return new FormatPlantFragment();
}
return null;
}
Otherwise, you should think about the profiles and implement a new logic to retrieve the correct tabs for each profile.
Related
I'm developing android screen for showing 2 different dashboard using tablayout & viewpager but only one screen shown at two different tabs(i.e. on MyDashboard & TeamDashboard).Please check my code.I'm new to android development.
I tried to use viewpager with pageradapter for swipe the screen.
public class ViewPagerAdapter extends FragmentPagerAdapter {
private int position;
private String tabTitles[] = new String[]{"MyDashboard", "Team` `Dashboard"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
// MyDashboard_Fragment dashboard_fragment=new `MyDashboard_Fragment();
// TeamDashboard_Fragment dashboard_fragment1=new `TeamDashboard_Fragment();
// position = position + 1;
// Bundle bundle=new Bundle();
// bundle.putString("message","Fragment :" +position);
// dashboard_fragment.setArguments(bundle);
// return dashboard_fragment;
Fragment fragment = null;
switch (position) {
case 0:
fragment = new MyDashboard_Fragment();
break;
case 1:
fragment = new TeamDashboard_Fragment();
break;
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
Problem is because you are not assigning the new value to position variable before use it, please update your code like bellow
public class ViewPagerAdapter extends FragmentPagerAdapter {
private int position;
private String tabTitles[] = new String[]{"MyDashboard", "Team` `Dashboard"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
// MyDashboard_Fragment dashboard_fragment=new `MyDashboard_Fragment();
// TeamDashboard_Fragment dashboard_fragment1=new `TeamDashboard_Fragment();
// position = position + 1;
// Bundle bundle=new Bundle();
// bundle.putString("message","Fragment :" +position);
// dashboard_fragment.setArguments(bundle);
// return dashboard_fragment;
position=i;
Fragment fragment = null;
switch (position) {
case 0:
fragment = new MyDashboard_Fragment();
break;
case 1:
fragment = new TeamDashboard_Fragment();
break;
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
Here the vector of what i'm trying to do
There Will be 2 tabs and one imageview or something between those two tabs, and the logo should be unclickable.
I've tried almost every approach but failed.
Anyone who knows how to do it?
Code :
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs2);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(1).setIcon(R.drawable.logo);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new LeaderBoard();
case 1:
return new Logo();
case 2:
return new Recent_Post();
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "LeaderBoard";
case 1:
return "";
case 2:
return "Recent Posts";
}
return null;
}
}
I've Solved my problem by switching the tabs between each other, Some Calculations.
int currentScreen = 0;
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 1) {
if(currentScreen == 0) {
mViewPager.setCurrentItem(2);
currentScreen = 2;
}else{
mViewPager.setCurrentItem(0);
currentScreen = 0;
}
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
I have implemented the first row of tab.
Inside that tab i have another set of Tabs.
Specifically two more tabs.
Please refer to these images.
The Picture above is the dog tab is selected.
The picture above is I want to select the Cat tab.
But unfortunately, the tab is not sliding properly.
its like its just a slowly scrolling.
but it should be on just one slide it should be on the cat tab.
My First layer of tab is inside Fragment.
The second row of tab is also in Fragment.
public class AdapterFragmentPagerItem extends FragmentStatePagerAdapter {
String[] pageTitle={"Do's","Dont's","First Aid"};
public AdapterFragmentPagerItem(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new FragmentDo();
case 1:
return new FragmentDonts();
case 2:
return new FragmentFirstAid();
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return pageTitle[position];
}
}
The above code is for the first image.
public class AdapterFragmentDos extends FragmentStatePagerAdapter {
String[] pageTitle = {"Dog", "Cat"};
public AdapterFragmentDos(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if(position==0){
return new FragmentDonts();
}else{
return new FragmentFirstAid();
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
return pageTitle[position];
}
}
And this is for the second image.
I have a FragmentPagerAdapter, and in the fragment class I have a public method that I need to use from main activity.
In this point I don't know how to access the fragment that contains that method. Can I modify something on the class below to set id or something to fragments?
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment_1();
case 1:
return new Fragment_2();
case 2:
return new Fragment_3();
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.fg_1).toUpperCase(l);
case 1:
return getString(R.string.fg_2).toUpperCase(l);
case 2:
return getString(R.string.fg_3).toUpperCase(l);
}
}
return null;
}
}
How can I set a Fragment id, or Fragment tag or something to acces the fragment later?
Any other way to access fragment methods could be good.
EDITED WITH MORE INFORMATION:
I'm really lost with this. But, I have a ViewPager too, these fragments are associated to actionBar Tabs.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
Is there any way to communicate with the fragments?
Like so :
switch (position) {
case 0:
Fragment f = new Fragment_1();
f.setId(...);
return f;
case 1:
...
}
This assumes you have a setId method in your fragment.
What I do however is define a static string TAG per fragment, I can then access it with Fragment_1.TAG.
I've got strange problem with FramentPageAdapter
MainActivity.java
#SuppressLint("ValidFragment")
public class MainActivity<DashboardActivity> extends FragmentActivity implements ActionBar.TabListener {
...
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(0)).setTabListener(this).setIcon(R.drawable.rating_good));
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(1)).setTabListener(this).setIcon(R.drawable.action_search));
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(2)).setTabListener(this).setIcon(R.drawable.action_search));
}
...
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int position) {
Fragment fragment = null;
switch(position) {
case 0:
fragment = new Fragment0();
break;
case 1:
fragment = new Fragment1();
break;
case 2:
fragment = new Fragment2();
break;
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
/*
* Title
*/
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section0).toUpperCase(l);
case 1:
return getString(R.string.title_section1).toUpperCase(l);
case 2:
return getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
public Fragment getItem(int position) return wrong position when i try to switch between 3 tabs. When i create app with 2 tabs only, everything work just fine. Adding more then 2 of them, creating strange problem.
Switch from 0 to 1 position - works fine, switch from 1 to 0 - works fine, switch from 1 to 2 posiition - works fine, but, when i try to go back from 2 to 1 position, public Fragment getItem(int position) - int position return "0" instead of "1".
Does anyone help me with this ?
Ok, I've found the solution.
First of all, the getItem "int position", doesn't indicate current display fragment.
To display 3 or more tabs, without unload firts fragment You must added this line:
mViewPager.setOffscreenPageLimit(3);
End of story...
You just need:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.d("test", "position = " + position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
The position in onPageSelected is what you want.
I had had the same problem, and I used fragment inside each tabs;
So instead of
mViewPager.setAdapter(new MainTabs(getFragmentManager()));
use this one:
mViewPager.setAdapter(new MainTabs(getChildFragmentManager()));
In case anyone else has this issue, I solved it by not using the position given. Instead, I get a List of Fragment with getSupportFragmentManager, loop through, and check if the current Fragment is instanceof my desired Fragment.