Skip to content
Zuzik edited this page Sep 23, 2017 · 4 revisions

FragmentNavigation


FragmentNavigation library will help you to organize navigation logic in you application by using fragments and pretty easy DSL.

Getting started

Create all fragments what you need

class TextFragment : Fragment() {
}

Create factories for these fragments that implement FragmentFactory interface

class TextFragmentFactory(private val text: String) : FragmentFactory {
     override fun create(path: List<String>) = TextFragment.create(text)
}

Create Schema object by using DSL. In this example I create and save it in Application class

class App : Application() {

    lateinit var scheme: Scheme<FragmentFactory>
        private set

    override fun onCreate() {
        super.onCreate()
        scheme = FragmentSchemeBuilder()
                .pager("a1", "b1") {
                    list("b1", "f1") {
                        child("f1", TextFragmentFactory("f1"))
                        child("f2", TextFragmentFactory("f2"))
                    }
                    list("b2", "d1") {
                        child("d1", TextFragmentFactory("d1"))
                        child("d2", TextFragmentFactory("d2"))
                    }
                    pager("b3", "e1") {
                        child("e1", TextFragmentFactory("e1"))
                        child("e2", TextFragmentFactory("e2"))
                    }
                }
    }
}

Implement NavigationFragmentContainer interface in your activity and display schema by using FragmentSchemePlaceholder class.

class MainActivity : AppCompatActivity(), NavigationFragmentContainer {
    override val scheme: Scheme<FragmentFactory>
        get() = app.scheme

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        FragmentSchemePlaceholder(scheme, supportFragmentManager, R.id.placeholder).show()
    }
}

To navigate in schema, add or remove elements you should you saved schema object.

Clone this wiki locally