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
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];
}
}
I try to build viewPager with several fragments.
But when I launch app I get crash with text
Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
my code is following:
public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 4;
MainFragment main;
CartFragment cart;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
main = new MainFragment();
cart = new CartFragment();
}
// Returns total number of pages
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return main;
case 1:
return null;
case 2:
return null;
case 3:
return cart;
default:
return null;
}
}
}
Use below code for calling Fragments...
#Override
public android.support.v4.app.Fragment getItem(int position) {
Fragment f = new Fragment();
switch (position) {
case 0:
f = new LogoFragment();
break;
case 1:
f = new AboutUsFragment();
break;
case 2:
f = new ServicesFragment();
break;
}
return f;
}
this is my way to use it :
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<PagerFragments> mFragments;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
this.mFragments = new ArrayList<>();
}
public void addFragment(Fragment fragment,String title) {
PagerFragments t = new PagerFragments();
t.setTitle(title);
t.setFragment(fragment);
mFragments.add(t);
notifyDataSetChanged();
}
public void addFragment(Fragment fragment) {
PagerFragments t = new PagerFragments();
t.setFragment(fragment);
mFragments.add(t);
notifyDataSetChanged();
}
//This method return the fragment for the every position in the View Pager
#Override
public Fragment getItem(int position) {
return mFragments.get(position).getFragment();
}
// This method return the titles for the Tabs in the Tab Strip
#Override
public CharSequence getPageTitle(int position) {
return mFragments.get(position).getTitle();
}
// This method return the Number of tabs for the tabs Strip
#Override
public int getCount() {
return mFragments.size();
}
}
PagerFragment.java :
import android.support.v4.app.Fragment;
public class PagerFragments {
Fragment fragment;
String title;
public Fragment getFragment() {
return fragment;
}
public void setFragment(Fragment fragment) {
this.fragment = fragment;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Then in fragment or activity :
private ViewPagerAdapter viewPagerAdapter;
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneDay), "1D");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneWeek), "1W");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneMonth), "1M");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneYear), "YTD");
viewPagerAdapter.addFragment(new RTopGainLossFragment(ytd), "1Y");
viewPagerAdapter.addFragment(new RTopGainLossFragment(max), "MAX");
viewpager.setAdapter(viewPagerAdapter);
tab.setupWithViewPager(viewpager);
I have an activity, which includes ViewPager with three fragments. In the second fragment i have ViewPager with two different fragments too.
This my MainActivityPagerAdapter which connected with ViewPager in MainActivity:
public class MainActivityPagerAdapter extends FragmentStatePagerAdapter implements LoginView.LoginStateChangeListener {
private static final String TAG = MainActivityPagerAdapter.class.getName();
private MapFragment mapFragment = new MapFragment();
private UserProfileFragment userProfileFragment = new UserProfileFragment();
private CompanyProfileFragment CompanyProfileFragment = new DiveCenterProfileFragment();
private NotificationsFragment notificationsFragment = new notificationsFragment();
public MainActivityPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return mapFragment;
case 1:
return notificationsFragment;
case 2:
switch (MyApplication.getInstance().getSharedPreferenceHelper().getActiveUserType()) {
case DIVECENTER:
return companyProfileFragment;
case DIVER:
case INSTRUCTOR:
return userProfileFragment;
case NONE:
return userProfileFragment;
}
default:
return null;
}
}
#Override
public int getCount() {
return 3;
}
#Override
public int getItemPosition(Object object) {
Log.i(TAG, "getItemPosition");
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
}
This my pager adapter for ViewPager at the second fragment:
public class NotificationsPagerAdapter extends FragmentStatePagerAdapter {
private PersonalNotificationsFragment personalNotificationsFragment = new PersonalNotificationsFragment();
private ActivityNotificationsFragment activityNotificationsFragment = new ActivityNotificationsFragment();
public NotificationsPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return personalNotificationsFragment;
case 1:
return activityNotificationsFragment;
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
public void setPersonalNotificationsFragment(PersonalNotificationsFragment personalNotificationsFragment) {
this.personalNotificationsFragment = personalNotificationsFragment;
}
public void setActivityNotificationsFragment(ActivityNotificationsFragment activityNotificationsFragment) {
this.activityNotificationsFragment = activityNotificationsFragment;
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
So, there are two user types in app:
Simple user
Company
When user changing his type i need to refresh all fragments in MainActivity including fragments inside second fragment. For this I'm using notifyDataSetChanged() for MainActivityViewPager, it work correctly, but fragments inside second fragment don't update(onCreateView don't called, and i see only empty views).
In what may be a problem?
Use getChildFragmentManager() for inner viewPager
Use getChildFragmentManager() instead of getFragmentManager() for changing fragments inside the viewPager
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.
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.