I use the navigation box in my project, it has 5 fragments. In one snippet, I have a design support library provided by TabLayout, which includes 2 snippets. Everything works fine, except when I leave the fragment that has TabLayout and switch back to it, all the content is gone.
In each fragment in TabLayout, I have a SwipeRefreshLayout with a ListView in it, they both do not display anything when switching.
I also noticed that TabLayout is starting to act weird. strange I mean that the white indicator starts to jump, you can hold it still in the middle of two tabs ...
So, did I miss something that TabLayout is acting so weird?
Code here: Snippet containing TabLayout:
public class MainFragment extends Fragment { private static String locale; private static ViewPager viewPager; private static TabLayout tabLayout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Utils.changeLanguage(getActivity()); final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.MyStyle); LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); View view = localInflater.inflate(R.layout.fragment1, container, false); viewPager = (ViewPager) view.findViewById(R.id.viewpager); tabLayout = (TabLayout) view.findViewById(R.id.tabs); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setupViewPager(viewPager); tabLayout.setupWithViewPager(viewPager); } private void setupViewPager(ViewPager viewPager) { LinesTabsAdapter adapter = new LinesTabsAdapter(getActivity().getSupportFragmentManager()); adapter.addFragment(new Fragment1(), "Fragment 1"); adapter.addFragment(new Fragment2(), "Fragment2"); viewPager.setAdapter(adapter); } public static class Fragment extends Fragment { private static List<RowItems1> rowItems; private static ListView listview; private static Adapter1 adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity1, container, false); listview = (ListView) view.findViewById(R.id.listview1); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); updateInfo(true); } public static void updateInfo() { rowItems = new ArrayList<>(); adapter = new Adapter1(context, rowItems); listview.setAdapter(adapter); } } public static class Fragment2 extends Fragment { private static List<RowItems2> rowItems; private static ListView listview; private static Adapter1 adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_lines_all, container, false); listview = (ListView) view.findViewById(R.id.activity2); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); updateInfo(true); } public static void updateInfo() { rowItems = new ArrayList<>(); adapter = new Adapter2(context, rowItems); listview.setAdapter(adapter); } } }
I use MainFragment as a singleton and replace it with the contents as soon as the user selects it in the navigation box. When I switch to it, everything works fine, the ListView contains its data, SwipeRefreshLayout works fine. When I switch to another fragment using the navigation box and switch back to this fragment, it does not display anything, as described above.
EDIT: Am I the only one who has problems with this? If someone had problems, how did they fix it?