How to get the current selected item position using TabLayout in Android Design Library

时光毁灭记忆、已成空白 提交于 2020-01-14 10:19:26

问题


I am using the android design library TabLayout in that how can I get the current selected item tab position.

  ViewPager pager = (ViewPager) view.findViewById(R.id.pager);      
  MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
  pager.setAdapter(adapter);
  tabLayout.setupWithViewPager(pager);

回答1:


As of version 22.2.1 of the Support library the TabLayout has a method getSelectedTabPosition.

Source 1 | Source 2




回答2:


You can call ViewPager's getCurrentItem() to get the index of the currently displayed item.




回答3:


  mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();
        }

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

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



回答4:


[Updated 29-07-2016]

Refer the accepted answer because below answer is deprecated. If you are using older version then refer it.

You can do it easily ...

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    pos = tab.getPosition();
                }

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

                }

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

                }
            });


来源:https://stackoverflow.com/questions/30758072/how-to-get-the-current-selected-item-position-using-tablayout-in-android-design

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