首页 > 解决方案 > 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.

标签: androidandroid-viewpagerandroid-tablayouttab-ordering

解决方案


使用 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) {

        }
    })

在此处输入图像描述在此处输入图像描述


推荐阅读