Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab change listener android

My HomeActivity extends AppCompatActivity that uses 2 tabs.

public class HomeActivity extends AppCompatActivity {      private SectionsPagerAdapter mSectionsPagerAdapter;     private TabLayout tabLayout;     ...      @Override     protected void onCreate(Bundle savedInstanceState) {          ...         mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());         tabLayout = (TabLayout) findViewById(R.id.tabs);         tabLayout.setupWithViewPager(mViewPager);     }      ... 

How to listen to tab change event? Let me know if I need to add any more code for clarity.

like image 750
jay Avatar asked Mar 27 '17 11:03

jay


2 Answers

You can use OnTabChangeListener.See below

    TabLayout tabLayout = new TabLayout(this);     tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {         @Override         public void onTabSelected(TabLayout.Tab tab) {             //do stuff here         }          @Override         public void onTabUnselected(TabLayout.Tab tab) {          }          @Override         public void onTabReselected(TabLayout.Tab tab) {          }     }); 

hope it help.

like image 195
Manpreet Singh Avatar answered Sep 30 '22 16:09

Manpreet Singh


Use the ViewPager.onPageChangeListener:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {          @Override         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {          }          @Override         public void onPageSelected(int position) {          }          @Override         public void onPageScrollStateChanged(int state) {          }     }); } 
like image 36
Abhishek Singh Avatar answered Sep 30 '22 14:09

Abhishek Singh