[英]android Kotlin: tab long click listener
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.我确信它可能是一些基本的东西,或者我根本不理解 tablayout 和寻呼机的点击。
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. 感谢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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.