Last active
December 16, 2018 14:25
-
-
Save adeds/109d8e7124ab1455aa27dd515d7451ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FragmentAdapter( | |
fragmentManager: FragmentManager | |
) : FragmentPagerAdapter(fragmentManager) { | |
val mFragmentList: ArrayList<Fragment> = ArrayList() | |
private val mFragmentTitleList: MutableList<String> = mutableListOf() | |
override fun getItem(position: Int): Fragment { | |
return mFragmentList[position] | |
} | |
override fun getCount(): Int { | |
return mFragmentList.count() | |
} | |
fun addFragment(fragment: Fragment, title: String = "") { | |
mFragmentList.add(fragment) | |
mFragmentTitleList.add(title) | |
} | |
override fun getPageTitle(position: Int): CharSequence { | |
return mFragmentTitleList[position] | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainFragment : Fragment() { | |
private val tabFrag by lazy { tab_frag } | |
private val pagerFrag by lazy { pager_frag } | |
private lateinit var pagerAdapter: FragmentAdapter | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
return inflater.inflate(R.layout.fragment_main, container, false) | |
} | |
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
pagerAdapter = FragmentAdapter(childFragmentManager) | |
pagerAdapter.addFragment(FirstFragment(), "First") | |
pagerAdapter.addFragment(ScndFragment(), "Second") | |
pagerAdapter.addFragment(ThirdFragment(), "Third") | |
pagerFrag.adapter = pagerAdapter | |
tabFrag.setupWithViewPager(pagerFrag) | |
tabFrag.setOnTabSelectedListener(OnTabCustomSelectedListener(context, tabFrag)) | |
val tabCount = tabFrag.tabCount | |
for (i in 0 until tabCount) { | |
val tab = tabFrag.getTabAt(i) | |
if (tab != null) { | |
val tabTextView = LayoutInflater.from(context).inflate(R.layout.item_tab_text, tabFrag, false) as TextView | |
tabTextView.text = tab!!.text | |
tabTextView.setTextAppearance(context, | |
if (i == 0) { | |
R.style.TextAppearance_Tabs_Selected | |
} else { | |
R.style.TextAppearance_Tabs | |
}) | |
tab.customView = tabTextView | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment