One PagerAdapter for multiple fragments - android

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.

Related

Call onCreateView() on fragment that is inside a viewpager

I have viewpager that has 4 fragments.
I have set the offscreenPageLimit to 4 and did not override getItemPosition of the adapter because I don't want them to be re-created every time when I change tabs.
Now, there will be a scenario when I want a particular fragment to be re-created (it means to call onCreateView).
That is before I select or when I select that fragment again.
How to do this?
Please try this adapter. This recover to re-create fragment again.
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private Fragment[] fragments;
private String[] titleList;
private int index;
public ViewPagerAdapter(FragmentManager fm, String[] titleList){ // titleList is list of title of fragments
super(fm);
fragments = new Fragment[titleList.length];
this.titleList = titleList;
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
if (fragments[position] == null){
YourFragment yourFragment = new YourFragment();
fragment = yourFragment;
fragments[position] = fragment;
return fragment;
}else{
return fragments[position];
}
}
#Override
public int getCount() {
return titleList.length;
}
#Override
public CharSequence getPageTitle(int position) {
return titleList[position].toUpperCase();
}
}

FragmentPagerAdapter crash

I try to build viewPager with several fragments.
But when I launch app I get crash with text
Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
my code is following:
public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 4;
MainFragment main;
CartFragment cart;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
main = new MainFragment();
cart = new CartFragment();
}
// Returns total number of pages
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return main;
case 1:
return null;
case 2:
return null;
case 3:
return cart;
default:
return null;
}
}
}
Use below code for calling Fragments...
#Override
public android.support.v4.app.Fragment getItem(int position) {
Fragment f = new Fragment();
switch (position) {
case 0:
f = new LogoFragment();
break;
case 1:
f = new AboutUsFragment();
break;
case 2:
f = new ServicesFragment();
break;
}
return f;
}
this is my way to use it :
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<PagerFragments> mFragments;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
this.mFragments = new ArrayList<>();
}
public void addFragment(Fragment fragment,String title) {
PagerFragments t = new PagerFragments();
t.setTitle(title);
t.setFragment(fragment);
mFragments.add(t);
notifyDataSetChanged();
}
public void addFragment(Fragment fragment) {
PagerFragments t = new PagerFragments();
t.setFragment(fragment);
mFragments.add(t);
notifyDataSetChanged();
}
//This method return the fragment for the every position in the View Pager
#Override
public Fragment getItem(int position) {
return mFragments.get(position).getFragment();
}
// This method return the titles for the Tabs in the Tab Strip
#Override
public CharSequence getPageTitle(int position) {
return mFragments.get(position).getTitle();
}
// This method return the Number of tabs for the tabs Strip
#Override
public int getCount() {
return mFragments.size();
}
}
PagerFragment.java :
import android.support.v4.app.Fragment;
public class PagerFragments {
Fragment fragment;
String title;
public Fragment getFragment() {
return fragment;
}
public void setFragment(Fragment fragment) {
this.fragment = fragment;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Then in fragment or activity :
private ViewPagerAdapter viewPagerAdapter;
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneDay), "1D");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneWeek), "1W");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneMonth), "1M");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneYear), "YTD");
viewPagerAdapter.addFragment(new RTopGainLossFragment(ytd), "1Y");
viewPagerAdapter.addFragment(new RTopGainLossFragment(max), "MAX");
viewpager.setAdapter(viewPagerAdapter);
tab.setupWithViewPager(viewpager);

Android FragmentPagerAdapter display moredata

I am using ViewPager,
I have to display more data in two fragments, but it does not show anything.
Is There an alternative layout to do it?
this is layout code:
<android.support.v4.view.ViewPager
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.8">
</android.support.v4.view.ViewPager>
this is adapter:
public class HitoeAdapter extends FragmentPagerAdapter {
private static HitoeAdapter adapter;
private final HeartRateFragment mHeartRateFragment;
private final ECGFragment mECGFragment;
public HitoeAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
mHeartRateFragment = new HeartRateFragment();
mECGFragment = new ECGFragment();
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return mHeartRateFragment;
case 1:
return mECGFragment;
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
}
Please help me.
Thanks.
Create the adapter class like below--
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Now add this method to set your fragments with viewpager -
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HeartRateFragment(), "HeartRateFrag");
adapter.addFragment(new ECGFragment(), "ECGFragment");
viewPager.setAdapter(adapter);
}

opening a layout (xml) inside a fragment instead of string text input

Problem: I was modifying a small app where three tabs are there in fragment but all refers to text string. I wanted to use layout xml so that I can make good graphics in it. How can I do it in this code:
Information_tab refers to a string where I can put text, but I want a layout.
public class AdapterInformation extends FragmentPagerAdapter {
public AdapterInformation(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
Bundle bundle = new Bundle();
int imgResId = 0;
int tab = 0;
int colorResId = 0;
switch (index) {
case 0:
tab = R.string.information_tab1;
break;
case 1:
tab = R.string.information_tab2;
break;
case 2:
tab = R.string.information_tab3;
break;
}
bundle.putInt("image", imgResId);
bundle.putInt("tab",tab);
bundle.putInt("color", colorResId);
SwipeTabFragment swipeTabFragment = new SwipeTabFragment();
swipeTabFragment.setArguments(bundle);
return swipeTabFragment;
}
#Override
public int getCount() {
return 3;
}}
you can add fragment layout like this.
private void setupViewPager(final ViewPager viewPager) {
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "Pending Upload");
adapter.addFragment(new TwoFragment(), "Complete Upload");
viewPager.setAdapter(adapter);
}
and your fragmentPagerAdapter code is like this:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Fragment getItem(int position)
{
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
and also add your setupViewPager method in onCreate()
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
setupViewPager(viewPager)
tabLayout.setupWithViewPager(viewPager);
Note: Do not use getItemPosition() method if its possible.. it
will produce redundancy of your listItems or ArrayList.. bcz every
time you swipe to other layout it will refresh page automatically..
its good if want this facility.. but you have write some code
regarding to handle this method.. and its also going to slow your
swipe animation if you have large amount of data.
For more information about tab layout and fragment: you can refer this one:
http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

How to structure the tabs in a tabHost in Android?

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;
}
}

Categories

Resources