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);
Related
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;
}
}}
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.
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'm making a score keeping app using tabs, tablayout, fragments, and a FragmentPagerAdapter. The app runs fine, but I can't figure out how I can access the fragments. After doing some work in the fragment, I want to access the fragment to update its score. How can I access the current fragment?
Edit - my main problem is finding the position. I can use tabLayout.getSelectedTabPosition() to get the correct position if I clicked on the tab, but if I swipe between tabs, viewpager handles it differently and I don't know how to find the position from that.
public class PagerAdapter extends FragmentPagerAdapter {
private int numTabs;
public PagerAdapter(FragmentManager fm, int tabs) {
super(fm);
numTabs = tabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return makeScoreFrag(1, "Team 1");
case 1:
return makeScoreFrag(2, "Team 2");
case 2:
return makeScoreFrag(3, "Team 3");
case 3:
return makeScoreFrag(4, "Team 4");
default:
return null;
}
}
#Override
public int getCount() {
return numTabs;
}
private ScoreFragment makeScoreFrag(int num, String title){
Bundle bundle = new Bundle();
bundle.putInt("score", 0);
bundle.putInt("number", num);
bundle.putString("title", title);
ScoreFragment frag = new ScoreFragment();
frag.setArguments(bundle);
return frag;
}
}
You can Override instantiateItem method in FragmentPagerAdapter
ScoreFragment[] fragments = new ScoreFragment[numTabs];
#Override
public Object instantiateItem(ViewGroup container, final int position) {
fragments[position] = (ScoreFragment) super.instantiateItem(container, position);
return fragments[position];
}
Then current fragment is:
ScoreFragment curFragment = fragments[curTab];
I am using FragmentPagerAdapter and I need to refresh the content after user action.
To do this, I am calling notifyDataSetChanged() on the adapter but nothing seems happen. I would like to avoid using FragmentStatePagerAdapter, since it contains just 3 pages.
Here is the override methods of the adapter:
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
switch (position) {
case PAGE_HOME:
fragment = HomeFragment.newInstance();
break;
case PAGE_CATEGORIES:
fragment = CategoriesFragment.newInstance();
break;
case PAGE_ACCOUNT:
if (account == null) {
fragment = AccountFragment.newInstance();
} else {
fragment = MyAccountFragment.newInstance();
}
break;
default:
fragment = null;
}
pageReferenceMap.put(position, fragment);
return fragment;
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
pageReferenceMap.put(position, fragment);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
pageReferenceMap.remove(position);
}
Thanks.
I found out that I have to return a different id for that page position. So here is how I return a unique id based on the fragment I want to display and not based on the page position:
#Override
public long getItemId(int position) {
long id;
switch (position) {
case HOME_PAGE_POSITION:
//fall through
case CATEGORIES_PAGE_POSITION:
id = position;
break;
case ACCOUNT_PAGE_POSITION:
if (account == null) {
id = position;
} else {
id = position + PAGE_COUNT;
}
break;
default:
id = -1;
}
return id;
}
If you have more that 2 fragments per pages to display at a time, you can just return position+PAGE_COUNT*n. Pretty easy.
Hope this help.
notifyDataSetChanged method will send a call back to your adapter , and check for the getCount method following this call back the getItem method is called. Your content in the screen refresh only if the content reflect any change in the currently selected fragment.The point to note here is that This callback will not select your first fragment. It will stay in the currently selected fragment.
For example:-
The currently selected fragment is 2, and you have changed the contents of fragment number 1 and makes a notifyDataSetChanged callback, so in this case you will not see any change as fragment 2 is currently present in the screen.