I am struggling with how to implement a long click on my tabs. I have tried a few different things however none seem to work.
I am sure maybe its something basic or I simply am not understanding the on clicks with the tablayout and pager.
I have found some questions on here but they do are not referring to on long click.
here are a few of the things that I have tried.
main_tab_pager.iterator().forEach {
view ->
view.setOnLongClickListener{
Toast.makeText(applicationContext, "....", Toast.LENGTH_LONG).show()
true
}
main_tabs.setOnLongClickListener {
when(main_tabs.selectedTabPosition) {
0-> Toast.makeText(applicationContext, "tab 0", Toast.LENGTH_LONG).show()
1-> Toast.makeText(applicationContext, "tab 1", Toast.LENGTH_LONG).show()
2-> Toast.makeText(applicationContext, "tab 2", Toast.LENGTH_LONG).show()
}
true
}
main_tab_pager.setOnLongClickListener {
when(main_tab_pager.currentItem) {
0-> Toast.makeText(applicationContext, "tab 0", Toast.LENGTH_LONG).show()
1-> Toast.makeText(applicationContext, "tab 1", Toast.LENGTH_LONG).show()
2-> Toast.makeText(applicationContext, "tab 2", Toast.LENGTH_LONG).show()
}
true
}
tab/pager setup code.
main_tab_pager.adapter = TabAdapter(supportFragmentManager, sections)
main_tabs.setupWithViewPager(main_tab_pager)
tab adapter
class TabAdapter(fragmentManager: FragmentManager, private val sections:
Array<BaseFragment>) : FragmentPagerAdapter(fragmentManager)
{
override fun getItem(position: Int): Fragment {
return sections[position]
}
override fun getCount(): Int {
return sections.size
}
override fun getPageTitle(position: Int): CharSequence? {
return sections[position].title
}
}
Solution thanks to Mike M.
val tabs = main_tabs.getChildAt(0) as LinearLayout
for (i in 0 until tabs.childCount) {
when(i){
0 -> tabs.getChildAt(0).setOnLongClickListener {
Toast.makeText(baseContext, "tab 0 ", Toast.LENGTH_LONG).show()
true
}
1 -> tabs.getChildAt(1).setOnLongClickListener {
Toast.makeText(baseContext, "tab 1", Toast.LENGTH_LONG).show()
true
}
2 -> tabs.getChildAt(2).setOnLongClickListener {
Toast.makeText(baseContext, "tab 2", Toast.LENGTH_LONG).show()
true
}
}
}
val tabs = recylerbin_tabLayout.getChildAt(0) as LinearLayout
for (i in 0 until tabs.childCount) {
tabs.getChildAt(i).setOnLongClickListener {
true
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.