How can i create dynamically tab with viewpager in android?

前端 未结 4 1190
别那么骄傲
别那么骄傲 2021-02-02 14:47

Explanation:

Suppose, I have multiple categories which come from my REST. I don\'t know how many categories it\'s mat be 5,7 or sometimes 2 etc. I want

4条回答
  •  温柔的废话
    2021-02-02 15:32

    if you want to dynamic tablayout and viewpager you can use this below code for a kotlin;

      for (i in 0 until tabLayout.size) {
            tabLayout.addTab(tabLayout.newTab().setText(tablayoutNameHere))
        }
    

    This area if you want non scrollable you can handle use it;

        var tabLayoutConfig = Runnable {
            if (tabLayoutPrivilege.width < resources.displayMetrics.widthPixels) {
                tabLayoutPrivilege.tabMode = TabLayout.MODE_FIXED
                val mParams = tabLayoutPrivilege.layoutParams
                mParams.width = ViewGroup.LayoutParams.MATCH_PARENT
                tabLayoutPrivilege.layoutParams = mParams
            } else {
                tabLayoutPrivilege.tabMode = TabLayout.MODE_SCROLLABLE
            }
        }
    
        tabLayoutPrivilege.post(tabLayoutConfig)
    
    
    
    
    val dynamicPrivilegeFragmentAdapter = DynamicPrivilegeFragmentAdapter(supportFragmentManager, tabCount,yourListHere)
            vpPrivilege.adapter = dynamicPrivilegeFragmentAdapter
    

提交回复
热议问题