android - Change order of tabs in TabLayout
问题描述
I have a typical TabLayout with a ViewPager attached to it so I can scroll through it by both swiping views and clicking on the tabs (just like presented here and cannot find a way of allowing users to reorder those tabs (and the corresponding pages)
I would like it to work like this: the user long presses one tab and drags it to the left and right, switching position with the others, just like you do with ItemTouchHelper
and RecycleView
.
Problem is, I don't find anything like ItemTouchHelper
for TabLayout
, if it even exists.
解决方案
使用 LTR RTL 更改 onTabSelected 内的 tablayout 订单位置。
subTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
if(tab.position == 0)
subTabLayout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR)
else
subTabLayout.setLayoutDirection(View.LAYOUT_DIRECTION_RTL)
subViewPager.currentItem = tab.position
}
override fun onTabUnselected(tab: TabLayout.Tab) {
}
override fun onTabReselected(tab: TabLayout.Tab) {
}
})
推荐阅读
- php - 如何通过file_get_content在html表中打印图像?
- php - 改善(模糊)相似性搜索结果的最佳方法?
- typescript - “this”类型的参数不可分配给 AWS CDK 中“Construct”类型的参数
- css - 背景图像与图像文件夹中的显示不完全相同
- html - HTML中的弹窗授权
- jupyter-notebook - JupyterLab in a psychology experiment where participants use JupyterLab
- spring - does anyone know how to transform this query from mongo to mongo template?
- sql - 在 SQL 中添加累积列
- ios - 使用 AVAudioPlayer 模拟免提电话以播放音频文件
- reactjs - 在 React Native 上构建自定义图表