Android Tab Layout get position number in onCreateVIew

312 views Asked by At

I want to get the current tab selected in android onCreateView function. I tried using Bundle to save information in the getItem function of the adapter. To test it out, I am using a toast to display the position number. But for some reason, for the first tab, it shows a position of -1 and then quickly changes to 0. For the other tabs it seems to be fine, except for the last one. No toast is shown for the last one.

1

There are 1 answers

0
Meng Tim On

I believe you are using getSelectedTabPosition() or some similar method in your code. This method will return "-1" if you do not select any tab. Otherwise it will return you the tab position. When you initialize your tab layout, there is no tab selected. I think in your code you pragmatically select one of the tab somewhere. So it will first return you "-1" (you select nothing yet) and when code run to where you select your tab(or after you click the tab to focus), it will show the position of the current selected tab(like 1 2 3 4 ...).