TabLayout tab selection

前端 未结 25 1508
情深已故
情深已故 2020-11-29 17:15

How should I select a tab in TabLayout programmatically?

 TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
 tabLayout.setupWithViewPager(viewPager         


        
25条回答
  •  忘掉有多难
    2020-11-29 17:37

    A bit late but might be a useful solution. I am using my TabLayout directly in my Fragment and trying to select a tab quite early in the Fragment's Lifecycle. What worked for me was to wait until the TabLayout finished drawing its child views by using android.view.View#post method. i.e:

    int myPosition = 0;
    myFilterTabLayout.post(() -> { filterTabLayout.getTabAt(myPosition).select(); });
    

提交回复
热议问题