I have an activity that holds my tabbed layout fragments. When I have 2 tabs everything works fine, but when I add a new tab I get
Caused by: java.lang.ClassCastException: rauhalamika.rcontrolble.HomeFragment cannot be cast to rauhalamika.rcontrolble.ManualFragment
Here is the SectionsPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
PresetsFragment presets = new PresetsFragment();
return presets;
case 1:
ManualFragment manual = new ManualFragment();
return manual;
case 2:
HomeFragment home = new HomeFragment();
return home;
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Presets";
case 1:
return "Manual";
case 2:
return "Home";
}
return null;
}
}
And the problem occurs when I'm calling ManualFragment's method from the activity like this:
((ManualFragment)getSupportFragmentManager().findFragmentById(R.id.container)).updatePressure(values);
This method updates a bunch of TextViews in the ManualFragment.
Everything works as it should if I only have PresetsFragment a ManualFragment, but when I add HomeFragment the app crashes.
What am I doing wrong?
When using FragmentPagerAdapter you can not get fragment by id.
getSupportFragmentManager().findFragmentById(R.id.container)
change this to
getSupportFragmentManager().findFragmentByTag("f1")
For tagging fragment Read This thread.
Related
When I open the application for the first time my tab is working fine. When I come back and go to the fragment page some times it returns null. Please help me ! What am I doing wrong? Here is my code.
public class TabsFragmentPagerAdapter extends FragmentPagerAdapter {
private ArrayList<ShareWinLeaderBoardTabModel> mShareWinLeaderBoardTabModelList;
public TabsFragmentPagerAdapter(FragmentManager fragmentManager, ArrayList<ShareWinLeaderBoardTabModel> shareWinLeaderBoardTabModelList) {
super(fragmentManager);
mShareWinLeaderBoardTabModelList = shareWinLeaderBoardTabModelList;
}
#Override
public Fragment getItem(int position) {
/*ShareWinLeaderBoardTabModel model = Once that is donemShareWinLeaderBoardTabModelList.get(position);
return new ShareWinLeaderBoardListFragment(model);*/
switch (position) {
case 0:
ShareWinLeaderBoardTabModel model = mShareWinLeaderBoardTabModelList.get(position);
return new ShareWinLeaderBoardListFragment(model);
case 1:
ShareWinLeaderBoardTabModel model1 = mShareWinLeaderBoardTabModelList.get(position);
return new ShareWinLeaderBoardListFragment(model1);
case 2:
ShareWinLeaderBoardTabModel model2 = mShareWinLeaderBoardTabModelList.get(position);
return new ShareWinLeaderBoardListFragment(model2);
case 3:
ShareWinLeaderBoardTabModel model3 = mShareWinLeaderBoardTabModelList.get(position);
return new ShareWinLeaderBoardListFragment(model3);
default:
return null;
}
}
#Override
public int getCount() {
return mShareWinLeaderBoardTabModelList.size();
}
}
When I press back there is some problem with the tab host. Tabs are coming fine but they are showing blank fragments. Any Help?
Image:
Change FragmentPagerAdapter to FragmentStatePagerAdapter
I'm using a view pager library in my application, where I use switch case with view pager. Here my code:
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return Slider_One_Fragment.newInstance();
case 1:
return Slider_Two_Fragment
.newInstance("Slider_Two_Fragment, Instance 1");
case 2:
return Slider_Three_Fragment
.newInstance("Slider_Three_Fragment, Instance 1");
default:
return new MainActivity();
}
}
#Override
public int getCount() {
return 3;
}
}
here the return type is of type "Fragment". But in default case I need to have a activity "MainActivity". Can anybody please help me to solving this issue?
the way the the ViewPager is setup it has to be a Fragment and not An Activity. One suggestion that you could do is move all the logic out of the MainActivity and into a MainFragment. then have your MainActivity contain the MainFragment.
I am finally catching up with the Android 4.0+ world and switching my activities to fragments. I am implementing the swipe view style tabs and need to be able to disable tabs based on an extra I am passing into the activity. My old way of doing this was this, but I can't seem to figure out how to do this with swipe views.
tabHost.getTabWidget().getChildAt(1).setVisibility(View.GONE);
The tabHost no longer applies. Now I have a FragmentPagerAdapter and ViewPager, but no clue how to use them to disable the tab. Thanks in advance for any pointers!
I managed to determine how to do this, however I don't know that it is the best way. What I had to do was pass a boolean value into my implementation of the FragmentPagerAdapter and only allow create the one fragment if I am in search mode.
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
boolean isSearching = false;
public SectionsPagerAdapter(FragmentManager fm, boolean isSearching) {
super(fm);
this.isSearching = isSearching;
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
Fragment fragment = null;
switch (position) {
case 0:
fragment = new UserGeneralFragment();
break;
case 1:
if (!isSearching) fragment = new CommentListFragment();
break;
}
return fragment;
}
#Override
public int getCount() {
if (!isSearching) return 2;
else return 1;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.activity_user_tab_general).toUpperCase(l);
case 1:
if (!isSearching) return getString(R.string.activity_user_tab_comments).toUpperCase(l);
break;
}
return null;
}
}
I have a section my class which is for handling what fragment to load for the FragmentPagerAdapter. Right now it loads fragments just fine but now I want to implement a FragmentActivity and I am having a problem figuring how to create it as I cant use "newinstance" since its type of Activity, and on top of it, I dont know how to call that FragmentActivity as its different from loading just a Fragment via "newinstance" method. My second tab is the one that I would like to be extended to FragmentActivity.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private Context _context;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
_context= getApplicationContext();
}
#Override
public Fragment getItem(int i) {
Fragment f = new Fragment();
switch(i){
case 0:
f=News.newInstance(_context);
break;
case 1:
f=Info.newInstance(_context);
break;
case 2:
f=Files.newInstance(_context);
break;
case 3:
f=Donate.newInstance(_context);
break;
}
/*
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
*/
return f;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.title_section1).toUpperCase();
case 1: return getString(R.string.title_section2).toUpperCase();
case 2: return getString(R.string.title_section3).toUpperCase();
case 3: return getString(R.string.title_section4).toUpperCase();
}
return null;
}
}
My guess is that I cant use getitem methods as that for only switching Fragments and that I would have to use something like:
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, new BasicFragment());
ft.commit();
Can someone help point me in the right direction?
Can someone help point me in the right direction?
This is not supported. It certainly is not possible with FragmentPagerAdapter, which only works with fragments.
is it somehow possible to have horizontal scrollable tabbar if there are more than e.g. 10 tabs in it?
Have anybody implemented something like this?
Mur
Ps.
It was not nice, what I did: I've deleted almost the same topic, I'd started yesterday. A big SORRY for man, who answered it already, even if it wasn't really the answer I'm looking for.
There actually is a way to implement this and it is called swipeable tabs layout. I have managed to use it in one of the apps I developed and published on Google Play. Here is the code to implement it:
SectionPagerAdapter class:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new HomeFragment();
case 1:
return fragment = new EventFragment();
case 2:
return fragment = new CoreTeamFragment();
case 3:
return fragment = new MapsFragment();
case 4:
return fragment = new FacebookFragment();
default:
break;
}
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;
}
}
Main class
public class CentruActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_centru);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// getActionBar();
}
public ActionBar getActionBar() {
return null;
}
}
Hope this helps :)