I have in MainActivity 3 (three) Fragments (HomeFragment, ChannelFragment and SearchFragment), with change made through click-only tabs. Inside the HomeFragment I have two (2) Fragments (VideosFragment and PlaylistFragment), this one with change in the click and tbm with a view pager.
It worked, but a problem came up after I added these last two fragments. When I go to the SearchFragment screen, it loads cool, but when I go back to the start screen, HomeFragment, this appears the tabs, but it gets all white, the contents of VideoFragment and PlaylistFragment are all white.
I get to see the logs, making requests, but simply does not appear the content.
I have already tried to re-create in onCreate, etc. without success.
Follow the methods for better understanding (MainActivity):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
if(savedInstanceState != null) {
tabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
public void init() {
initializeTabHost();
initializeViewPager();
hideShowKeyboards(false);
}
private void initializeViewPager() {
List<Fragment> fragments = new ArrayList<>();
HomeFragment homeFragment = new HomeFragment();
fragments.add(homeFragment);
ChannelsListFragment channelsListFragment = new ChannelsListFragment();
fragments.add(channelsListFragment);
SearchFragment searchFragment = new SearchFragment() {
fragments.add(searchFragment);
FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
viewPagerCustom = (ViewPagerCustom) super.findViewById(R.id.main_viewPagerCustom);
viewPagerCustom.setAdapter(fragmentAdapter);
viewPagerCustom.addOnPageChangeListener(new PagerChangerListener());
}
public void initializeTabHost() {
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
icHome = new ImageView(this);
icHome.setImageResource(R.mipmap.ic_home_clicked);
icChannel = new ImageView(this);
icChannel.setImageResource(R.mipmap.ic_channel);
icSearch = new ImageView(this);
icSearch.setImageResource(R.mipmap.ic_search);
addTab(this, this.tabHost, this.tabHost.newTabSpec("Tab 1").setIndicator(icHome));
addTab(this, this.tabHost, this.tabHost.newTabSpec("Tab 2").setIndicator(icChannel));
addTab(this, this.tabHost, this.tabHost.newTabSpec("Tab 3").setIndicator(icSearch));
tabHost.setOnTabChangedListener(new TabHostChangeListener());
}
HomeFragment Methods:
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container == null)
return null;
rootView = inflater.inflate(R.layout.fragment_home, container, false);
init();
Log.i("Ciclo", "onCreateView() sendo chamado HomeFragment");
return rootView;
}
private void init() {
initTabHost();
initializeVariables();
initializeViewPager();
}
private void initTabHost() {
fragmentTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab 1").setIndicator("VÍDEOS"),
VideosFragment.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab 2").setIndicator("PLAYLISTS"),
PlaylistFragment.class, null);
fragmentTabHost.setOnTabChangedListener(new OnTabChangeListener());
fragmentTabHost.getTabWidget().setBackgroundResource(R.color.icomptv_blue);
//fillColor();
fillColorLine();
//Personaliza texto tabHost
for (int i = 0; i < fragmentTabHost.getTabWidget().getChildCount(); i++) {
TextView tv = (TextView) fragmentTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#FFFFFF"));
tv.setTextSize(15);
}
}
private void initializeViewPager() {
List<Fragment> fragments = new ArrayList<>();
VideosFragment videosFragment = new VideosFragment();
fragments.add(videosFragment);
PlaylistFragment playlistFragment = new PlaylistFragment();
fragments.add(playlistFragment);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getFragmentManager(), fragments);
viewPager = (ViewPager) rootView.findViewById(R.id.main_viewPager);
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(new PagerChangerListener());
}
VideoFragment's Oncreate:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null)
return null;
Log.i("Ciclo", "onCreateView() do VideoFragment");
view = inflater.inflate(R.layout.fragment_videos, container, false);
initMethods();
return view;
}
Good people, who can tell me why this error? and propose a thank you solution