Skip to content Skip to sidebar Skip to footer

How To Reference A Fragment's Methods From An Activity?

I have a viewpager app that I am working on with 3 fragments. I am adding the fragments dynamically via the FragmentStatePagerAdapter like that: public class ViewPagerAdapter ex

Solution 1:

After a lot of experimentation I came with a simple solution to my problem. Since I am adding the fragments dynamically (in the viewPagerAdapter) and not in XML I couldn't reference them weither by id or tag. My solution is to modify slightly the adapter so that it stores a reference of the fragment passed as a parameter. Then all of the methods of the fragment are accessible (if declired public) in the mainActivity:

MainActivity:

publicclassMainActivityextendsAppCompatActivity {
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Current","Hourly","Daily"};
intNumboftabs=3;
Current_forecast_fragment mCurrent_forecast_fragment;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //-----------MY CODE STARTS HERE-----------------this.mCurrent_forecast_fragment = newCurrent_forecast_fragment();

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  newViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs,mCurrent_forecast_fragment);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setOffscreenPageLimit(3);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width// Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(newSlidingTabLayout.TabColorizer() {
        @OverridepublicintgetIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);
}

My Code in ViewPagerAdapter:

publicclassViewPagerAdapterextendsFragmentStatePagerAdapter {
private Current_forecast_fragment mCurrent_forecast_fragment;
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is createdint NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created// Build a Constructor and assign the passed Values to appropriate values in the classpublicViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb,Current_forecast_fragment current_fragment) {
    super(fm);
    this.mCurrent_forecast_fragment = current_fragment;
    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;

}

//This method return the fragment for the every position in the View Pager@Overridepublic Fragment getItem(int position) {

    if(position == 0) // if the position is 0 we are returning the First tab
    {
        returnthis.mCurrent_forecast_fragment;
    }
    elseif (position == 1)            // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
    {
        returnnewHourly_forecast_fragment();
    }else {
        returnnewDaily_forecast_fragment();
    }

}
}

Post a Comment for "How To Reference A Fragment's Methods From An Activity?"