Error: Only TabItem instances can added to TabLayout (Kotlin)
Opened this issue · 1 comments
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)
}
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!