I have sample project with TabLayout
and PagerAdapter
.
Strange things happens with TabLayout when I call pagerAdapter.notifyDataSetChanged();
you can directly set the current postion.
you can get the selected position before notifying dataset change and save it in static varaible and update it everytime:
public static tabPostion;
@Override
public void onTabSelected(TabLayout.Tab tab) {
tabPostion = tab.getPosition();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
//set the postion of the pervious tab
yourviewpager.setCurrentItem(tabPostion);
below is the code from my project :
@Override
public void onTabSelected(TabLayout.Tab tab) {
// imageHashMap = new HashMap<>();
onClearOrSelectAllImage(FlickQuickEnum.PIC_SELECTION_ACTION.CLEAR);
selectedImageUrls = new HashMap<>();
String tag = tab.getText().toString();
String tagName = tag.substring(1, tag.length());
currentTab = tagName;
if (tagName != null) {
dbAlbumPhotoList = new ArrayList<>();
if (tagName.equals("All")) {
dbAlbumPhotoList = dbAlbumPhotosHashMap.get(album.getAlbumName());
} else {
dbAlbumPhotoList = dbAlbumPhotosHashMap.get(tagName);
}
}
updatePhotoCount();
setPhotosSelectedActions(false);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}