Fragmentstatepageradapter - Getitem
Solution 1:
Try commenting out all of the code beneath:
TabLayouttabLayout= (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
in your onCreate method. Run again and see what happens.
Your switch statement is laid out in a strange way, too. If you'll definitely only have 5 cases, then it should go from case 0-4, with a default case at the end that prints an error to the log, or throws an exception. I don't think this will be causing your issue, but it's good practice.
Also it's usually not a good idea to instantiate fragments using the default constructor. A better way is to use a static factory method, ie:
Tab1Fragmentfragment= Tab1Fragment.create();
return fragment;
With a static create() method in your fragment, that initialises the object. It gives you a single access point to fragment, and means that any initialisation you need to do (now or at some point down the line) can all be done in once place, making you less prone to error. Android also re-creates fragments using the default constructor, so any initialisation you come to do in an overloaded constructor will be ignored. Again I don't think this is the cause of your issue, but it's worth bearing in mind.
Edit: In addition, when you say it is choosing random fragments "The position will show 0,3,2,0,4,1 just random numbers", do you mean those are the fragments being displayed on screen, or are those the positions you're logging from the "getItem(int position)" argument?
Solution 2:
viewPager = (ViewPager) findViewById(R.id.tabPager);
adapter = newViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(newFragment1(),"fragment 1");
adapter.addFragment(newFragment2(),"fragment 2");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(newViewPager.OnPageChangeListener() {
@OverridepublicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
return position;
}
}
@OverridepublicvoidonPageSelected(int position) {
}
@OverridepublicvoidonPageScrollStateChanged(int state) {
}
});
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
assert tabLayout != null;
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
Then in your Adapter
classViewPagerAdapterextendsFragmentPagerAdapter {
privatefinal List<Fragment> mFragmentList = newArrayList<>();
privatefinal List<String> mFragmentTitleList = newArrayList<>();
publicViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Overridepublic Fragment getItem(int position) {
return mFragmentList.get(position);
}
@OverridepublicintgetCount() {
return mFragmentList.size();
}
publicvoidaddFragment(Fragment fragment,String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Overridepublic CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Post a Comment for "Fragmentstatepageradapter - Getitem"