Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Only TabItem instances can added to TabLayout (Kotlin) #56

Open
LamourBt opened this issue Aug 16, 2017 · 1 comment
Open

Error: Only TabItem instances can added to TabLayout (Kotlin) #56

LamourBt opened this issue Aug 16, 2017 · 1 comment

Comments

@LamourBt
Copy link

LamourBt commented Aug 16, 2017

Hello,
First for most, @fabiendevos this sort of MVP design rocks man ... 🥇 , you saved live man 💯

Tell me if I'm wrong:

Magellan idea is that every screen has its view, and when it comes to the tabBar the same logic is applied, and that means there is a TabBarScreenView attached to TabBarScreen, then you have the Adapter providing all the screens that needs to be shown.

BaseTabsScreen

 typealias MyScreen = Screen<*>
  typealias MyList = List<MyScreen>

   class  BaseTabsScreen(screens: MyList): ScreenGroup< MyScreen,TabsView>(screens) {

    override fun createView(context: Context?): TabsView {
        return TabsView(context,super.getScreens()) // or screens
    }
    
    override fun onShow(context: Context?) {
        super.onShow(context)
        Log.i("screen counts", super.getScreens().size.toString())
        for (each in super.getScreens()) {
            view.addTabView(each.getView())
        }
    }
}

TabsView

there error is on addTabView, I try to convert the view.context into a TabItem but no luck

class TabsView(context:Context?):BaseScreenView<BaseTabsScreen>(context) {
    private var tabLayout: TabLayout? = null
    private var viewPager: ViewPager? = null

    constructor(context:Context?,tabScreens: MyList ) : this(context) {
        View.inflate(context,R.layout.tabs_layout, this)
        setup()
        initPager(context,tabScreens)
    }

    private fun setup() {
        tabLayout = findViewById(R.id.tabLayout)
        viewPager = findViewById(R.id.pager)
    }

    private fun initPager(context:Context?, tabScreens:MyList) {
        var adapter = TabsAdapter(context, tabScreens)
        pager.adapter = adapter
        pager.offscreenPageLimit = Int.MAX_VALUE
        pager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))

        tabLayout?.setupWithViewPager(pager)

        tabLayout?.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(pager))
    }

      fun addTabView(view:View) { 
          //  Only TabItem instances can be added to TabLayout
        //   pager.addView(TabItem(view.context)) // Try to convert the context of that view to item
          pager.addView(view)

      }
      // addView is calling addViewInternal 

}

Adapter

class TabsAdapter(context: Context?, tabsScreens:MyList): PagerAdapter() {
     private var _context:Context? = null
     private var _tabsScreens:MyList

    init {
        _context = context
        _tabsScreens = tabsScreens
    }

    override fun instantiateItem(container: ViewGroup?, position: Int): Any = _tabsScreens[position].getView()

    override fun isViewFromObject(view: View?, `object`: Any?): Boolean = view == `object`

    override fun getCount(): Int = _tabsScreens.size

    override fun getPageTitle(position: Int): CharSequence = _tabsScreens[position].getTitle(_context)

}

@fabiendevos
Copy link
Contributor

Thanks! Glad you like this design.

Here is a working implementation of tabs (in Java for now), that might help you: https://github.com/wealthfront/magellan/wiki/Implementing-Tabs-or-other-%22Screens-into-a-Screen%22-UI%2C-using-ScreenGroup

We plan on releasing tab support as an add-on library soon. Stay tuned!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants