Why tabs and fragments not showing up? - android

For some reason my Tabs are not showing up.
My player home activity should display two tabs naming Profile and Teams but it is not showing anything. And also not showing any error.
OnCreate Player home Activity
mainAppToolBar = (android.support.v7.widget.Toolbar) findViewById(R.id.mainAppBar);
setSupportActionBar(mainAppToolBar);
getSupportActionBar().setTitle("Player Home");
playerTabLayout = (TabLayout) findViewById(R.id.playerMainTabs);
playerViewPager = (ViewPager) findViewById(R.id.playerMainTabPager);
playerSectionPageAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
playerViewPager.setAdapter(playerSectionPageAdapter);
playerTabLayout.setupWithViewPager(playerViewPager);
Code for Section Page Adapter
public class SectionPageAdapter extends FragmentPagerAdapter {
public SectionPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
ProfileFragment profileFragment = new ProfileFragment();
return profileFragment;
case 1:
TeamFragment teamFragment = new TeamFragment();
return teamFragment;
default:
return null;
}
}
#Override
public int getCount() { return 2;}
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile";
case 1:
return "Teams";
default:
return null;
}
}}
Please help.

getCount() is overriden, but I don't see that getPageTitle is. I think you're missing that #override.
#Override
public int getCount() { return 2;}
#Override - add this
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Profile";
case 1:
return "Teams";
default:
return null;
}
}}

Related

Back Button on WebView Fragments - not going to previous page

I have WebView in Fragments and in one Fragment just some buttons (not WebView). When Back Button is pressed, I intend to go back to previous page on WebView (on the rest, or when there is no previous page, closing the App is fine). After going through several suggestions here, finally I have come up to this point, but it does not lead to the previous page on pressing Back Button.
I am not sure if it has to do with addToBackStack as in this Stack Overflow post; I am also not sure how to make that work in my case.
public class MyFragment extends Fragment {
public WebView webView;
public boolean WebViewGoBack() {
if(webView.canGoBack()){
webView.goBack();
return true;
}
return false; //webview cannot go back, so use the method of the BackButton
}
}
One Fragment is implemented like this:
public class Tab1Fb extends MyFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1fb, container, false);
webView =(WebView)rootView.findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webView.loadUrl("https://www.facebook.com/");
return rootView;
}
}
The MainActivity, I have pageradapter and overridden methods:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private Fragment mCurrentFragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1Fb tab1 = new Tab1Fb();
return tab1;
case 1:
Tab2Videos tab2 = new Tab2Videos();
return tab2;
case 2:
Tab3Downloads tab3 = new Tab3Downloads();
return tab3;
case 3:
Tab4Twitter tab4 = new Tab4Twitter();
return tab4;
case 4:
Tab5Contact tab5 = new Tab5Contact();
return tab5;
case 5:
Tab6Support tab6 = new Tab6Support();
return tab6;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 6;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "FACEBOOK";
case 1:
return "VIDEOS";
case 2:
return "DOWNLOADS";
case 3:
return "TWITTER";
case 4:
return "CONTACT";
case 5:
return "JOIN";
}
return null;
}
public Fragment getCurrentFragment() {
return mCurrentFragment;
}
#Override
public void setPrimaryItem(View container, int position, Object object) {
if (getCurrentFragment() != object) {
mCurrentFragment = ((Fragment) object);
}
super.setPrimaryItem(container, position, object);
}
}
#Override
public void onBackPressed() {
switch (mViewPager.getCurrentItem()) {
case 0:
if (!webViewGoBack(0)) {
//do something if webview cannot go back
}
break;
case 1:
break;
default:
}
}
public boolean webViewGoBack(int num) {
SectionsPagerAdapter adapter = ((SectionsPagerAdapter)mViewPager.getAdapter());
MyFragment f = (MyFragment )adapter.getCurrentFragment();
if (f!= null) {
return f.WebViewGoBack();
}
return false;
}

Unable to add 4th Fragment to my Tabbed bar in my Tabbed Activity Project

i try to add a new fragment to my Tabbed Bar and did the following like i did for the Fragments before:
1. I created a Fragment and the layout for that
2. I added the Fragment name in my MainActivity:
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).
switch (position) {
case 0:
locations_fragment loc = new locations_fragment();
return loc;
case 1:
qr_code qra = new qr_code();
return qra;
case 2:
HTW_Fragment htw = new HTW_Fragment();
return htw;
case 3:
Events_Fragment pre = new Events_Fragment();
return pre;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Locations";
case 1:
return "QR Code Scanner";
case 2:
return "HTW";
case 3:
return "Events";
}
return null;
}
}
if i try to start my App it shows only the first three Fragment in my Tab Bar.
The reason is actually simple. You said to adapter there is only 3 pages.
:)
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
Replace it with return 4; and enjoy your 4-th page displayed.

Reload fragments inside ViewPager which inside another fragment inViewPager

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

How to Convert an Activity to a Fragment that Utilizes getSupportFragmentManager

I am attempting to convert an Activity to a Fragment but I am having the issue of getSupportFragmentManager cannot be resolved and my getDrawable's as well. I am at a lost of where to go from here. Any help with code example will be greatly appreciated.
My Attempted Fragment:
public class HomeFragment extends Fragment {
private MaterialViewPager mViewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_home, container, false);
mViewPager = (MaterialViewPager) rootView.findViewById(R.id.materialViewPager);
mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
#Override
public Fragment getItem(int position) {
switch (position % 4) {
//case 0:
// return RecyclerViewFragment.newInstance();
//case 1:
// return RecyclerViewFragment.newInstance();
//case 2:
// return WebViewFragment.newInstance();
default:
return RecyclerViewFragment.newInstance();
}
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position % 4) {
case 0:
return "Projects";
case 1:
return "Missions";
case 2:
return "Areas";
case 3:
return "Flights";
}
return "";
}
});
mViewPager.setMaterialViewPagerListener(new MaterialViewPager.Listener() {
#Override
public HeaderDesign getHeaderDesign(int page) {
switch (page) {
case 0:
return HeaderDesign.fromColorResAndDrawable(
R.color.colorPrimaryDark,
getDrawable(R.drawable.kenburns_cityview));
case 1:
return HeaderDesign.fromColorResAndDrawable(
R.color.colorPrimaryDark,
getDrawable(R.drawable.kenburns_glendalough));
case 2:
return HeaderDesign.fromColorResAndDrawable(
R.color.colorPrimaryDark,
getDrawable(R.drawable.kenburns_goldengatebridge));
case 3:
return HeaderDesign.fromColorResAndDrawable(
R.color.colorPrimaryDark,
getDrawable(R.drawable.kenburns_dubai));
}
return null;
}
});
mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount());
mViewPager.getPagerTitleStrip().setViewPager(mViewPager.getViewPager());
return rootView;
}
}
From this (original) Activity:
public class MainActivity extends AppCompatActivity {
private MaterialViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (MaterialViewPager) findViewById(R.id.materialViewPager);
FoldingTabBar tabBar = (FoldingTabBar) findViewById(R.id.folding_tab_bar);
mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
#Override
public Fragment getItem(int position) {
switch (position % 4) {
//case 0:
// return RecyclerViewFragment.newInstance();
//case 1:
// return RecyclerViewFragment.newInstance();
//case 2:
// return WebViewFragment.newInstance();
default:
return RecyclerViewFragment.newInstance();
}
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position % 4) {
case 0:
return "Projects";
case 1:
return "Missions";
case 2:
return "Areas";
case 3:
return "Flights";
}
return "";
}
});
mViewPager.setMaterialViewPagerListener(new MaterialViewPager.Listener() {
#Override
public HeaderDesign getHeaderDesign(int page) {
switch (page) {
case 0:
return HeaderDesign.fromColorResAndDrawable(
R.color.colorPrimaryDark,
getDrawable(R.drawable.kenburns_cityview));
case 1:
return HeaderDesign.fromColorResAndDrawable(
R.color.colorPrimaryDark,
getDrawable(R.drawable.kenburns_glendalough));
case 2:
return HeaderDesign.fromColorResAndDrawable(
R.color.colorPrimaryDark,
getDrawable(R.drawable.kenburns_goldengatebridge));
case 3:
return HeaderDesign.fromColorResAndDrawable(
R.color.colorPrimaryDark,
getDrawable(R.drawable.kenburns_dubai));
}
return null;
}
});
mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount());
}
}
Getting the support FragmentManager is a method of AppCompatActivity, so in your support Fragment you'd need getActivity().getSupportFragmentManager(). You can reference the getDrawable method in a similar fashion.
You can also call getFragmentManager of the fragment if it is the direct child of activity.

getCount() function of 2 ViewPagers are conflicting

I have two ViewPagers First have 5 Views (Fragments) and Second Have 3 Fragments inside a single java file. The problem is when second view shows Fragment 1st then i can only access 3 Fragments of First ViewPager 1-3 and when second View Pager is showing 3rd Fragment then can access only 3-5 fragment means can access only 3 fragment at a time.
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
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 DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
//Log.e("Position", position+"");
if(position==0){
fragment=new AssetSelectionFragmentWallpaper();
}
else{
fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
args.putInt(DummySectionFragment.ARG_OBJECT, position+1);
fragment.setArguments(args);
}
return fragment;
}
#Override
public int getCount() {
// Show 5 total pages.
return 5;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
case 4:
return getString(R.string.title_section5).toUpperCase(l);
}
return null;
}
#Override
public int getItemPosition(Object object) {
if (object instanceof UpdateableFragment) {
((UpdateableFragment) object).update(null);
}
if(object instanceof AssetSelectionFragmentWallpaper){
return POSITION_NONE;
}
return super.getItemPosition(object);
}
}
public class SectionsPagerAdapter1 extends FragmentStatePagerAdapter {
private Fragment fragment;
private Swipe sw;
public SectionsPagerAdapter1(FragmentManager fm, Swipe s) {
super(fm);
this.sw = s;
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
fragment = new DummySectionFragment1();
Bundle args = new Bundle();
args.putInt(DummySectionFragment1.ARG_SECTION_NUMBER, position + 1);
args.putInt(DummySectionFragment1.ARG_OBJECT, position+1);
fragment.setArguments(args);
return fragment;
}
#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 "SETTINGS";
case 1:
return "SHOP";
case 2:
return "CONTACTS";
case 3:
return "";
}
return null;
}
#Override
public int getItemPosition(Object object) {
// don't return POSITION_NONE, avoid fragment recreation.
return super.getItemPosition(object);
}
}
Every Object Name is Different and Globally Declared...
I think getCount() Function is conflicting to each other.
Please Help to resolve this...
Thanks...
I think you should check your XML first, if there you find any problem. or
You can fix also by declaring any RONDOM ID like _pager.setId(int);

Categories

Resources