Tab change listener android

后端 未结 6 1545
我寻月下人不归
我寻月下人不归 2021-01-07 16:22

My HomeActivity extends AppCompatActivity that uses 2 tabs.

public class HomeActivity extends AppCompatActivity {

    private SectionsPagerAdapter mSection         


        
6条回答
  •  被撕碎了的回忆
    2021-01-07 17:03

    You can use addOnTabSelectedListener method:

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                if (tab.getPosition() == 0) {
                    toolBarTitle.setText("Tab one");
                } else if (tab.getPosition() == 1) {
                    toolBarTitle.setText("Tab two");
                } else {
                    toolBarTitle.setText("Tab three");
                }
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
    
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
            }
        });
    

提交回复
热议问题