Reputation: 81
I want to know or someone to give me an idea to detect which position I am on a tab layout, i got a tab layout and i cant seem to figure out where i should do what i want, i tried doing it here at an extended FragmentPagerAdapter:
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
Basically i check if im at position 1 and do what ever and then do something else but it doesnt work, only calls the code once and neva again ANd here at an extended fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
switch (p) {
case 1:
view = RenderFactory.getClass("Page1", inflater, container).displayContent();
break;
case 2:
view = RenderFactory.getClass("Page2", inflater, container).displayContent();
break;
case 3:
view = RenderFactory.getClass("Page3", inflater, container).displayContent();
break;
}
return view;
}
here i just put the code within each case and the code doesnt get called more then once. Any ideas ??
Upvotes: 0
Views: 42
Reputation: 624
Try to with addOnTabSelectedListener. This event handler gives you the position of the chosen tab.
Example:
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Upvotes: 1
Reputation: 322
You can get the current item with this code :
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab){
int current_position = tab.getPosition();
}
});
or :
int current_position = mViewPager.getCurrentItem();
Upvotes: 0