androidconductor

ViewPager with Conductor Controller


I'm trying to use the Conductor's viewPager(RouterPagerAdapter) and everything works fine. The problem occurs when I try to push another controller using the router instance of the current Controller.

It's the Adapter for the view pager.

class ReaderAdapter(host: Controller) : RouterPagerAdapter(host) {
    override fun configureRouter(router: Router, position: Int) {
        if (!router.hasRootController()) {
            when (position) {
                0 -> router.setRoot(RouterTransaction.with(FeedController()))
                1 -> router.setRoot(RouterTransaction.with(RssController()))
            }
        }
    }

    override fun getPageTitle(position: Int) = if (position == 0) "FEED" else "RSS"

    override fun getCount() = 2

}

It's the view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/feedTab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        tools:targetApi="lollipop" />

    <android.support.v4.view.ViewPager
        android:id="@+id/feedPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

The way that I'm initializing the adapter:

class HostReaderController : BaseController() {

    companion object {
        const val TAG_UP_TRANSACTION = "TAG_UP_TRANSACTION"
    }

    override fun onInject() {
    }

    override fun onAttach(view: View) {
        feedPager?.adapter = ReaderAdapter(this)
        feedTab?.setupWithViewPager(feedPager)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View {
        return inflater.inflate(R.layout.host_reader_view, container, false)
    }

    override fun onDestroyView(view: View) {
        feedPager?.adapter = null
        feedTab?.setupWithViewPager(null)

        super.onDestroyView(view)
    }

    override fun getEnableBackAction() = false

    override fun getTitleToolbar() = ""
}

Now I can explain better the problem. When the view is RSSController() which is a Tab, there is a button for sharing content so I have to open a new controller ShareController like this:

rssController.router.pushController(RouterTransaction.with(ShareController()))

If I do that the ShareController is created inside the view pager as a view of RSSController(). What I want is to create a new view outside of the viewPager not in. I did a trick to solve this problem:

   mainActivity.router.pushController(RouterTransaction.with(ShareControler))

If I try to push a new controller using the router of FeedController and RssController the view is created as a child of the controller who started. How can I fix that?

Thanks :D


Solution

  • What you're seeing is correct. Each page has its own router, so anything you push is going to end up on that page, not in the parent container. What you'd probably want to do is something like parentController.router.pushController().