Tab change listener android

后端 未结 6 1546
我寻月下人不归
我寻月下人不归 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 16:43

    Depends on what you want. If you just want to know if the tab page changes, or find out which tab position was selected, use:

    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
        public void onPageScrollStateChanged(int state) {
    
        }
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
        }
    
        public void onPageSelected(int position) {
    
        }
    });
    

    Else, if you want to keep track of the tab itself, you will have to implement TabLayout.OnTabSelectedListener like this:

    public class HomeActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener
    

    And add this methods to your HomeActivity class:

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

提交回复
热议问题