How to scroll tablayout programmatically - Android

前端 未结 13 2135
终归单人心
终归单人心 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 01:01

    I found this solution for me:

        TabLayout tabLayout = activity.getTabLayout();
        tabLayout.setSmoothScrollingEnabled(true);
        tabLayout.setScrollPosition(targetChannelPosition, 0f, true);
    

    Also, if you receive this error: "Only the original thread that created a view hierarchy can touch its views.", you can use this code, in order to run on Ui thread:

        // find a way to get the activity containing the tab layout
        TabLayout tabLayout = activity.getTabLayout();
        activity.runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                TabLayout.Tab tab = tabLayout.getTabAt(targetChannelPosition);
                tab.select();
            }
        });
    
    0 讨论(0)
提交回复
热议问题