Search code examples
androidlistviewandroid-fragmentsandroid-design-libraryandroid-tablayout

Android TabLayout does not display contents anymore as soon as fragment is switched


I am using a navigation drawer in my project, with 5 fragments in it. In one fragment I have the in the design support library introduced TabLayout, which includes 2 Fragments. All works fine, except when I leave the fragment which has the TabLayout and switch back to it, all the content is gone.

In each fragment in the TabLayout I have a SwipeRefreshLayout with a ListView in it, they both don't display anything upon switch.

I also noticed the TabLayout starts acting strange. by strange I mean the white indicator starts jumping, you are able to hold it still in the middle of the two tab...

So am I missing something that the TabLayout acts so strange?

The code here: Fragment which contains the 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 am using the MainFragment as a singleton and replacing the content with it as soon as the user selects it in the navigation drawer. When I switch to it, everything works fine, the ListView contains its data, the SwipeRefreshLayout works fine. When I switch to another fragment using the navigation drawer and switch back to this fragment, it displays nothing, as described above.

EDIT: Am I the only one having trouble with this? If anyone had problems, how did they fix it?


Solution

  • When I used viewPager and back on it, I had empty fragments until switch when I used getSupportFragmentManager.

    Try to use: getChildFragmentManager.