How to scroll tablayout programmatically - Android

前端 未结 13 2166
终归单人心
终归单人心 2021-02-07 00:31

I have created 30 scrollable tabs using tablayout.

So first three tabs are visible on screen and rest of them are invisible which can be scroll using swipe gesture.

13条回答
  •  粉色の甜心
    2021-02-07 00:58

    I found the solution.

    First I had found the width of tablayout and scroll it's x position to width and than called the select() method of last tab.

    And it works fine.

    Below is the code.

    mTabLayout.setScrollX(mTabLayout.getWidth());
    mTabLayout.getTabAt(lastTabIndex).select();
    

    Updated:

    If above is not working you can use the below code as well, it is also working fine.

    new Handler().postDelayed(
            new Runnable() {
                @Override public void run() {
                    mTabLayout.getTabAt(TAB_NUMBER).select();
                }
            }, 100);
    

提交回复
热议问题