Tabs Inside Tab using PagerSlidingTabStrip - android

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.

Related

One PagerAdapter for multiple fragments

I have a basic PagerAdapter:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public PagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Main tab1 = new Main();
return tab1;
case 1:
Secondary tab2 = new Secondary();
return tab2;
case 2:
Third tab3 = new Third();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return 3;
}
}
It works perfectly for one tablayout and 3 fragments which are displayed in the viewpager. I have multiple of the same fragment design (same tabs) across a few fragments. Is it possible to use this one PagerAdapter with multiple options (fragments), so all it has to change is the classes that are instantiated.
Main tab1 = new Main(); to SomeOtherClass tab1 = new SomeOtherClass();. I tried doing this with parameters and variables but no success.
In the public Fragment getItem(int position); method, you should return the fragment in the mFragmentListfor the given position. Something like this:
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
By this way you can use this adapter for any Fragment.
Also, the method getCount() should be as follow:
#Override
public int getCount() {
return mFragmentList!=null ? mFragmentList.size() : 0;
}
In order to avoid hardcoded values.

PagerSlidingTabStrip with Icon And Text

I was able to successfully implement PagerSlidingTabStrip with my app.
But my application requires a tab with Text And Icons.
Unfortunately, I am not able to do that.
public class Pager extends FragmentStatePagerAdapter implements PagerSlidingTabStrip.IconTabProvider {
private int tabIcons[] = {R.drawable.ic_explore, R.drawable.ic_nearby, R.drawable.ic_road_rescue};
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
public Pager(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Explore();
case 1:
return new Nearby();
case 2:
return new RoadRescue();
}
return null;
}
#Override
public int getCount() {
return tabIcons.length;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
#Override
public int getPageIconResId(int position) {
return tabIcons[position];
}
}
What seems to be wrong with my code? Thanks

Android - smoothScrollToPosition inside a child Fragment in a ViewPager

I'm having difficulty in setting smooth scroll for Fragments inside ViewPager, nested in another Fragment which acts as their parent.
I set up this method :
public void scrollToTop()
{
this.recyclerView.smoothScrollToPosition(0);
}
For Fragments not in a ViewPager, but I don't know how to get the reference of Fragments inside a ViewPager.
I did
BottomLobiFragment activeLobi = (BottomLobiFragment)getSupportFragmentManager().findFragmentByTag(utilities.lobbyTag);
to get the parent Fragment.
Code for a ViewPager (The rest are almost identical, difference is only at number of item, and constructor)
public class LobbyPagerAdapter extends FragmentStatePagerAdapter {
public LobbyPagerAdapter (FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return Friends.newInstance("Friends, Instance 1");
// case 1: return Photo.newInstance("Photo, Instance 1");
// case 2: return Activities.newInstance("Activities, Instance 1");
default: return Friends.newInstance("Friends, Default");
}
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length];
}
#Override
public int getCount() {
//return 3;
return CONTENT.length;
}
private class setAdapterTask extends AsyncTask<Void,Void,Void> {
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
}
}
}
Note : Every adapter is located inside the parent Fragment
First modify your Adapter to this
public class LobbyPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> mFragments = new ArrayList<>();
public LobbyPagerAdapter (FragmentManager fm) {
super(fm);
mFragments.add(Friends.newInstance("Friends, Instance 1"));
mFragments.add(Photo.newInstance("Photo, Instance 1"));
mFragments.add(Activities.newInstance("Activities, Instance 1"));
}
#Override
public Fragment getItem(int pos) {
return mFragments.get(pos);
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length];
}
#Override
public int getCount() {
//return 3;
return CONTENT.length;
}
private class setAdapterTask extends AsyncTask<Void,Void,Void> {
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
}
}
}
Now in your fragment or activity which is holding the ViewPager & the adapter instance, you can simply do like adapter.getItem(1); example :
//Suppose your LobbyPagerAdapter instance looks like this in your code
LobbyPagerAdapter adapter = new LobbyPagerAdapter(getFragmentManager());
// And now you want to get the fragment at second position in your tab
Photo photoFragmentObject = (Photo)adapter.getItem(1);
// And now you want to get the fragment at first position in your tab
Friends friendsFragmentObject = (Friends)adapter.getItem(0);

how not to show some tabs on tabbed activity?

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.

How to structure the tabs in a tabHost in Android?

I'm crating a tabHost to contain all the content of my Android application in 3 tabs, should i create the content of the tab inside its linear layout or create the tab content in a separate fragment and include it in the tab layout?
And how to include the fragment inside a layout also?
Excuse me i'm a bit beginner to this.
you just add viewpagger for calling fragment
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "Fragment1", "Fragment2", "Fragment2" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public Fragment getItem(int position) {
//return SuperAwesomeCardFragment.newInstance(position);
switch (position) {
case 0:
return new JavaFragment1(); //this call you fragment1
case 1:
return new JavaFragment2(); //this call you fragment2
case 2:
return new JavaFragment3(); //this call you fragment3
}
return null;
}
}

Categories

Resources