I want to build a preference tab for my app but since I have other tabs extending from a regular fragment, I have an incompatible types error, as a PreferenceFragment cannot be converted to Fragment.
Here is my Tab Fragment Adapter :
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1Discover tab1 = new Tab1Discover();
return tab1;
case 1:
Tab2Planning tab2 = new Tab2Planning();
return tab2;
case 2:
Tab3Favorites tab3 = new Tab3Favorites();
return tab3;
case 3:
Tab4Messages tab4 = new Tab4Messages();
return tab4;
case 4 :
/*Tab5Profile tab5 = new Tab5Profile();
return tab5;*/
return new PreferenceFragment() {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
};
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
There are incompatible types, the problem is - your fragments inherited from support.v4.app.Fragment, but PreferenceFragment inherited from android.app.Fragment.
There are two ways you could deal with it:
Inherit all your fragments from android.app.Fragment
Reimplement PreferenceFragment by taken it source code from here for example and inherit it from support.v4.app.Fragment
Related
I have created two fragments in my app. I want to use these fragments in viewpager. This is my CustomAdapter for Viewpager:
public class CustomSwipeAdapter extends FragmentStatePagerAdapter {
public CustomSwipeAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return new fragmentComment();
case 1:
return new fragment_Rating();
default:
return new fragmentComment();
}
}
#Override
public int getCount() {
return 2;
}
}
And this is my Viewpager:
pager=(ViewPager)findViewById(R.id.view_pager);
FragmentActivity fm=new FragmentActivity();
CustomSwipeAdapter adapter=new CustomSwipeAdapter(fm.getSupportFragmentManager());
pager.setAdapter(adapter);
But when i run my app,nothing is shown in viewpager.
Can Anyone help?
You forgot
adapter.addFragment(new fragmentComment(), "FragmentComment");
adapter.addFragment(new fragment_Rating(), "FragmentRating");
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'm using tablayout for my app. I have to set my middle tab as my opening tab. I'm using 3 tabs in my layout. How can I make it possible?
I'm using viewpage adapter and it has switch case for selection of tabs:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
Facebook_Tab tab2;
Mukil_FM_Tab tab1;
Shows_List_Tab tab3;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
Facebook_Tab tab;
switch (position) {
case 1:
tab2 = new Facebook_Tab();
return tab2;
case 0:
tab1 = new Mukil_FM_Tab();
return tab1;
case 2:
tab3 = new Shows_List_Tab();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
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;
}
}
I am using a FragmentPagerAdapter to swipe three Fragments. It's not giving me any errors, but it always loads the next Fragment in advance. How can I stop this behavior?
Here is my FragmentPagerAdapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Context _context;
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
_context = context;
}
#Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
switch (position) {
case 0:
f = ReservationTab.newInstance(_context);
break;
case 1:
f = GalleryTab.newInstance(_context);
break;
case 2:
f = VideoTab.newInstance(_context);
break;
}
return f;
}
#Override
public int getCount() {
return 3;
}
}
try to use this
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2 /*count of your fragments in viwePager*/);
Try using setOffscreenPageLimit(0) on your ViewPager