android.support.design.widget.TabLayout select Tab Programmatically

三世轮回 提交于 2019-11-30 21:37:49

问题


I am using android.support.design.widget.TabLayout. It has two tabs, If user selects second tab On particular condition I want user to redirect to first tab and disallow him to go to sencond tab until condition matches. To achieve this I tried,

tabLayout.getTabAt(0).select(); 

but it does not reselect first tab


回答1:


This is because that view is still not initialized properly, and you are trying to perform some action.

As a solution you just need to put one hadler before selecting perticular tab.

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



回答2:


This is how I solved it:

tabLayout.getTabAt(CurrentItem).getCustomView().setSelected(true);



回答3:


This worked for me:

int tabIndex = 2;
tabLayout.setScrollPosition(tabIndex,0f,true);
viewPager.setCurrentItem(tabIndex);



回答4:


This is my setup. works fine for me.

      //declare your tabs to be add on
        TabLayout tlDailyView;
        private TabLayout.Tab tabAppointment, tabSlots;


    @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_daily_view, container, false);
            initializeMembers();
            setupTabLayout();
            return view;
        }


    private void setupTabLayout() {
            tlDailyView.addTab(tabAppointment, 0, true);
            tlDailyView.addTab(tabSlots, 1, true);
            tlDailyView.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {

                    switch (tab.getPosition()) {
                        case 0:
                        //open fragment at position 0 here
                        case 1:
                        //open fragment at position 1 here
                            break;
                    }

                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });
        }

 private void initializeMembers() {
        tabSlots = tlDailyView.newTab();
        tabAppointment = tlDailyView.newTab();
        tabAppointment.setText(R.string.tab_appts).select();
        tabSlots.setText(R.string.tab_slots);
    }

don't forget to initialize your tab layout above.




回答5:


You can select the tab in Fragment.onViewCreated().




回答6:


tabLayout.getTabAt(index specific).select();



回答7:


It's too late but it might help other developers.

If you go inside and see how tabLayout.getTabAt(tabIndex).select(); has been implemented inside. You'll come to know that, it sends your flow to

onTabReselected(tab: TabLayout.Tab?)

and your flow doesn't go to

onTabSelected(tab: TabLayout.Tab?)

if your current tab and tabIndex are same. So this is very important to consider.




回答8:


Mihir's answer gave me an idea to try this. Seems like it works without the hardcoded timer, and also correctly updates the scroll for selected tab:

final TabLayout tabLayout = ...;
tabLayout.postOnAnimation(new Runnable() {
    @Override
    public void run() {
        tabLayout.getTabAt(2).select();
    }
});


来源:https://stackoverflow.com/questions/31891178/android-support-design-widget-tablayout-select-tab-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!