Search code examples
androidandroid-holo-everywhere

How to get currently visible fragment from activity when using holoeverywhere slider addon?


I want to know how to perform these actions in holoeverywhere:

Get reference to the currently visible and active fragment from the activity when using slider addon in combination with tabber.

Get reference to TabsTabsSwipeFragment from the activity/other fragments and dynamically disable/enable swiping.


Solution

  • Get a reference to the currently visible and active Fragment from the Activity when using slider addon in combination with tabber:

    You can provide a tag for a Fragment like this:

    sliderMenu.add("tab2", Fragment2.class, SliderMenu.GREEN).setTag("mynavigation-2");
    

    Also you can get the current page number using:

    sliderMenu.getCurrentPage();
    

    You have no way of obtaining the current Fragment directly, but you can combine these two methods and find a Fragment via the FragmentManager:

    getSupportFragmentManager().findFragmentByTag("mynavigation-" + sliderMenu.getCurrentPage());
    

    Get a reference to the TabsTabsSwipeFragment.java from the Activity/other Fragments and dynamically disable/enable swiping. In your code:

    getSupportFragmentManager().findFragmentByTag("mynavigation-2").getChildFragmentManager().findFragmentById(tagFragmentId);
    

    In your Activity:

    private int tagFragmentId;
    public void setTagFragmentId(int i) {
        Log.i(TAG, "fetched setTagFragmentId: " + i);
        tagFragmentId= i;
    }
    

    In your tab Fragment (which is a child of TabsTabsSwipeFragment):

       public void onViewCreated(View view, Bundle savedInstanceState) 
            ((mainActivity) getSupportActivity()).setTagFragmentId(this
                    .getId());
        }
    

    Reference: https://github.com/Prototik/HoloEverywhere/wiki/Addon:-Slider