Change order of tabs in TabLayout

475 views Asked by At

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.

1

There are 1 answers

0
Nimesh Patel On

Use LTR RTL To change tablayout Order Position inside onTabSelected.

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

        }
    })

enter image description hereenter image description here