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
Related
I am passing data to a fragment using View-Pager Adapter.
My data is coming until View-Pager Adapter but is not showing in the fragment.
this is my view pager adapter code.
public class ExensesViewPagerAdapter extends FragmentPagerAdapter {
private Context mContext;
private MonthsTotal monthsTotal;
public ExensesViewPagerAdapter(FragmentManager fm, Context mContext, MonthsTotal monthsTotal) {
super(fm);
this.mContext = mContext;
this.monthsTotal = monthsTotal;
Logger.e("ExensesViewPagerAdapter");
Logger.json(new Gson().toJson(monthsTotal));
}
/*public ExensesViewPagerAdapter(FragmentManager fm) {
super(fm);
}*/
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return AmountFragment.newInstance(monthsTotal);
case 1:
return GraphFragment.newInstance(monthsTotal);
}
return null;
}
This is my fragment code where I am getting the data.
public static AmountFragment newInstance(MonthsTotal monthsTotal) {
Logger.e("AmountFragment");
Logger.json(new Gson().toJson(monthsTotal));
AmountFragment fragment = new AmountFragment();
Bundle args = new Bundle();
args.putParcelable("allExpenseData", monthsTotal);
fragment.setArguments(args);
return fragment;
}
Please help me to solve.Thank you in advance.
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 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
I am unable to create dynamic layout for each tabs, i am able to create tabs dynamically by this constructor(public MyPagerAdapter(FragmentManager fm , int noOfTabs)), but coudn't inflate the view for each tabs creating for each fragment which should be dyanmic.
public class MyPagerAdapter extends FragmentPagerAdapter
{
private final String[] TITLES = { "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
"Top New Free", "Trending" };
int noOfTabs;
**public MyPagerAdapter(FragmentManager fm , int noOfTabs)
{
super(fm);
this.noOfTabs = noOfTabs;
}**
/*public MyPagerAdapter(FragmentManager fm)
{
super(fm);
}*/
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return noOfTabs; //TITLES.length;
}
#Override
public Fragment getItem(int position) {
return SuperAwesomeCardFragment.newInstance(position);
}
}
I am facing problem here which should be dynamic and not static as three fragments(FragmentA, FragmentB , FragmentC), it can be any number of fragments depending on the int noOfTabs:
#Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position) {
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
case 2:
fragment = new FragmentC();
break; }
return fragment;
}
Thanks in advance if any one could help me in this, as i am using this with help of library PagerSlidingTabStrip.
#nbokmans solution may be okay but i've used a container to accomplish that.
Creating a simple host fragment with FrameLayout inside and by replacing fragment was a good dynamic solution for me.
Using Otto EventBus just made it awesome because it can be changed from everywhere with just simple post
#Subscribe
fun changeFragment(type: FragmentTypeEvent){
val ft = activity.supportFragmentManager.beginTransaction()
val frag = getFragment(type)
ft.replace(R.id.container_frame,frag)
ft.commit()
}
I am using ViewPager and loading multiple fragment in Fragment Activity using FragmentPage Adapter. I want to access a fragment which contains an ImageButton. Can any one help me to access Imagebutton from fragment in fragment activity?
Here is my code :
Fragment Activity
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_gallery);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (CustomPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setOnPageChangeListener(this);
pagerLayout = (LinearLayout) findViewById(R.id.tour_pager_lay);
mPageControl = (PageControl)findViewById(R.id.horizontal_pager);
mPageControl.addPagerControl(3);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ImageFragment(R.drawable.landing_logo);
case 1:
return new ImageFragment(R.drawable.welcome_postit);
case 2:
return new LoginFragment();
case 4:
return new RegisterFragment();
default:
return null;
}
}
}
Suppose that your ImageButton is on your RegisterFragment, then you can do:
RegisterFragment tmp = ((RegisterFragment) ((MyAdapter) mPager.getAdapter()).instantiateItem(mPager, 4));
//your ImageButton should be public or you can create a getter or something, I assume that it's public
tmp.imageButton.setText("foo");