I am using a TabHost with TabWidget and inside a ViewPager. For the ViewPager I created my FragmentPagerAdapter and inside I put some Fragments. In every Fragment I have a RecyclerView to display information vertical and horizontal.
The Fragment is updating when I change from one tabwidget but not when the Activity is loaded.
Graphics Behaviour
For example I check my products list I pickup on I buy this product and then in other tab the list of my products should be listed but it doesn't, when I change the tab and return to my previous tab then the view is update and the products list is showing.
Ok here is the first tab where I select a product, In tab One, I have a ViewPager with a FragmentPagerAdapter. The list of products is a RecyclerView. When I select a Product, an Activity is launch then I buy the product.
When I buy the product I return to my main Activity (TabHost container) I and should see the product bought in the list but the list is empty.
When I change the tab and return to list tab then fragment is showed. I don't know here is my error.
Code of main Activity
It is my main activity code:
public class AppTabActivity extends AppCompatActivity
implements ViewPager.OnPageChangeListener,
TabHost.OnTabChangeListener {
public static final String TAB_INDEX = "TAB_INDEX";
ViewPager pager;
TabPagerAdapter tabsPagerAdapter;
TabHost tabHost;
Toolbar topToolBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_main_layout);
topToolBar = (Toolbar) findViewById(R.id.idTopToolBar);
setSupportActionBar(topToolBar);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
String[] tabNames = getResources().getStringArray(R.array.tabs_name);
for (String tab : tabNames) {
TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec(tab);
tabSpec.setIndicator(tab);
tabSpec.setContent(new FakeView(getApplicationContext()));
tabHost.addTab(tabSpec);
}
tabHost.setOnTabChangedListener(this);
pager = (ViewPager) findViewById(R.id.viewPager);
if(tabsPagerAdapter == null )
tabsPagerAdapter = new TabPagerAdapter(getSupportFragmentManager());
pager.setAdapter(tabsPagerAdapter);
tabsPagerAdapter.notifyDataSetChanged();
pager.addOnPageChangeListener(this);
Intent from = getIntent();
//when i buy a product i send the tab list index of my bought products
int currentIndex = from.getIntExtra(TAB_INDEX, 1);
pager.setCurrentItem(currentIndex);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tabHost.setCurrentTab(position);
pager.setCurrentItem(position);
tabsPagerAdapter.notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onTabChanged(String tabId) {
int selectedItem = tabHost.getCurrentTab();
pager.setCurrentItem(selectedItem);
}
class FakeView implements TabHost.TabContentFactory {
Context context;
public FakeView(Context ctx) {
context = ctx;
}
@Override
public View createTabContent(String name) {
View view = new View(context);
view.setMinimumHeight(0);
view.setMinimumWidth(0);
return view;
}
}
}
TabPagerAdapter
I use a list of my Fragments.
public class TabsPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentList;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
fragmentList = new ArrayList<>();
fragmentList.add(new ProductFragment());
fragmentList.add(new BuyListFragment());
fragmentList.add(new DetailFragment());
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Tips
Each fragment implements its view using a layout. The layouts contains some widgets and one of them are RecyclerView. I only share the TabHost, ViewPager and Adapter code because I think the problem is here, When I change the tabs the list is paint. I am lost, I don't know were is the problem.

