TabController

Additional

Language
Java
Version
N/A
Created
Jan 14, 2017
Updated
Aug 10, 2017
Owner
Appolica
Contributors
Deyan Genovski (deangenovski)
bkolarov-appolica
AlexanderI
3
Activity
Badge
Generate
Download
Source code
APK file

Announcement

TabControllerAndroid by Appolica

This library provides an easy to use API for switching between fragments that share the same container. Implementing tabs, such as the ones Instagram has at the bottom of the layout, is a much simpler goal to achieve when using TabController. Tabs can be switched by using either show/hide or attach/detach. You can even provide your own implementation of the way tabs are being shown.

The library is developed and maintained by Appolica.

Note: The sample uses BottomBar in order to give a nice example of using this library. TabController is for managing fragments and does not provide any UI views.

Download

Gradle

dependencies {
    ...
    compile 'com.appolica:tab-controller:1.0.3'
}

Example

There are two ways of using this library. One is to use TabControllerFragment. It encapsulates all the fragments and their container into a single fragment.

You can use it like any other fragment by adding it to the xml layout:

<fragment
        android:id="@+id/fragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        class="com.appolica.tabcontroller.fragment.TabControllerFragment"/>

or instantiating it and adding it by yourself:

final TabControllerFragment controllerFragment = new TabControllerFragment();
getSupportFragmentManager()
                .beginTransaction()
                .add(conainerId, controllerFragment)
                .commitNow();

Obtain the TabController instance simply like this:

final TabController tabController = controllerFragment.getTabController();

The other way is to provide your own fragment container. If you choose to use this, you have to create the TabController instance by yourself:

finalTabController tabController =newTabController(getSupportFragmentManager(), R.id.container);


It doesn't matter whether you use your own container or TabControllerFragment. Once you obtain a TabController instance, everything is the same.

In order to show your fragment you have to use

TabController::switchTo method. This method accepts an implementation of FragmentProvider. Suppose you have multiple fragments and the first one is called HomeFragment. Then you would need something like HomeFragmentProvider, that implements FragmentProvider.publicclassHomeFragmentextendsFragment { publicstaticfinalStringTAG="HomeFragment"; publicstaticHomeFragmentgetInstance(inttabNum) { finalHomeFragment fragment =newHomeFragment(); return tab; } ... }
classHomeFragmentProviderimplementsFragmentProvider{ @OverridepublicStringgetTag() { returnHomeFragment.TAG; } @OverridepublicFragmentgetInstance() { returnHomeFragment.getInstance(); } }

Create an instance of

HomeFragmentProvider and pass it to TabController::switchTo in order to show your HomeFragment.finalHomeFragmentProvider homeProvider =newHomeFragmentProvider(); tabController.switchTo(homeProvider);

The

TabController will create a new instance (by calling FragmentProvider::getInstance) of your fragment if it hasn't been already created. This depends on whether FragmentManager::findFragmentByTag returns null for the tag, given by FragmentProvider::getTag.