TabsPagerAdapter How to start with case 1 or another? - Android - android

I have 12 page in tabspageradapter and I don't want to start with case 0. For example I want to start with case 4. How can I do that?
This is my adapter.
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new JanuaryFragment();
case 1:
return new FebFragment();
case 2:
return new MarchFragment();
case 3:
return new AprFragment();
case 4:
return new MayFragment();
case 5:
return new JuneFragment();
case 6:
return new JulyFragment();
case 7:
return new AugustFragment();
case 8:
return new SepFragment();
case 9:
return new OctFragment();
case 10:
return new NovFragment();
case 11:
return new DecFragment();
default:
return null;
}
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 12;
}
}
Thanks for your help in advance :)

Related

ClassCastException when new Fragment is added to SectionsPagerAdapter

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.

setCurrentItem() on ViewPager

I create a custom view pager like this:
public class TaskPagerAdapter extends FragmentStatePagerAdapter {
public TaskPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new NotificationFragment();
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
return fragment;
}
#Override
public int getCount() {
return 5;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
and used on main fragment :
mPagerAdapter = new TaskPagerAdapter(getFragmentManager());
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(-2);
in the setCurrentItem I have to which number to use until that i do not get an nullpoiner error?I want NotificationFragment() class is first start on view pager
Edit
whit mViewPager.setCurrentItem(-1); i got blow error:
Process: safarayaneh.com.anotify.debug, PID: 21484
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:120)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1248)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:18788)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1028)...
Read setCurrentItem
Set the currently selected page. If the ViewPager has already been
through its first layout with its current adapter there will be a
smooth animated transition between the current item and the specified
item.
Problem
mViewPager.setCurrentItem(-1);
Don't set -1 or -2 . index starts from 0 .
EDIT
switch (position) {
case 0:
fragment = new NotificationFragment();
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
return fragment;
WHY 1,2,3,4 BLANK . You should try with
switch (position) {
case 0:
fragment = new NotificationFragment();
break;
}
return fragment;
mViewPager.setCurrentItem(0);
Since NotificationFragment has the first position.
It's index is 0.
Therefore, it should be something like mViewPager.setCurrentItem(0);

How do I tap to move to the next ViewPager page fragment?

I have a ViewPager setup that's been working fine for my app but it only lets me swipe the screen to move to the next slide. What I want is to also be able to move to the next slide by tapping the screen. Is this possible?
I've been trying to figure this out for a week but I couldn't find the right code to make it work.
Here's what I've done so far:
public class JoyfulHappyfuntime extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_joyful_happyfuntime);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return new BoopTheName();
case 1: return new Creed();
case 2: return new Waffles();
case 3: return new Smilea();
case 4: return new Smileb();
case 5: return new Smilec();
case 6: return new PancakesOne();
case 7: return new FirstJoyfulHappyfuntime();
case 8: return new Waffles();
case 9: return new Smilea();
case 10: return new Smileb();
case 11: return new Smilec();
//etc
default: return new Continue();
}
}
#Override
public int getCount() {
return 74;
}
}
}
You can try one of those :
Adding a transparent view ontop where you handle the onClick to change
the position to the next.
using a ViewPagerIndicator on top to let the user click on the section he wants
View Pager Tutorial

how to replace a fragment within a ListViewPagerAdapter.java

I'm using ViewPager in a fragment. It has 3 tabs
Here's the code of my fragmentOagerAdapter:
#Override
public Fragment getItem(final int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = Fragment.instantiate(this.context, ClassA.class.getName());
break;
case 1:
fragment = Fragment.instantiate(this.context, ClassB.class.getName());
break;
case 2:
fragment = Fragment.instantiate(this.context, ClassC.class.getName());
break;
}
return fragment;
}
#Override
public CharSequence getPageTitle(final int position) {
String res = "";
switch (position) {
case 0:
res = "nameA";
break;
case 1:
res = "nameB";
break;
case 2:
res = "nameC";
break;
}
return res;
}
#Override
public int getCount() {
return 3;
}
I'm adding a a button in my main fragment that will allow me to switch between a list display and a grid display.
So what I want is to have a dynamic fragment depending on some enum stored within the Preferences
I want to do this for the 1st tab (position 0)
So I do the following:
#Override
public Fragment getItem(final int position) {
Fragment fragment = null;
switch (position) {
case 0:
switch (Settings.getInstance().getProductDisplayMode()) {
case LARGE_GRID:
fragment = Fragment.instantiate(this.context, ClassALargeGrid.class.getName());
break;
case SMALL_GRID:
fragment = Fragment.instantiate(this.context, ClassASmallGrid.class.getName());
break;
case LIST:
default:
fragment = Fragment.instantiate(this.context, ClassA.class.getName());
break;
}
break;
case 1:
fragment = Fragment.instantiate(this.context, ClassB.class.getName());
break;
case 2:
fragment = Fragment.instantiate(this.context, ClassC.class.getName());
break;
}
return fragment;
}
...
So it works fine when first opening the activity, but I don't know how to switch the fragment displayed in the first tab dynamically.
I tried calling destroyItem() on the FragmentPagerAdapter but it doesn't help...
Any idea ?
I actually had this problem a while back. I wound up having to take the fragment out of the viewpager and then use the FragmentManager to swap out the fragments dynamically.
Then, when I wanted to switch to another "page," I wrote some custom animations to "slide in" a fragment that hosted a ViewPager
Not a pretty solution, but it worked.

Android scrollable Tabbar

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 :)

Categories

Resources