Android ListView items disappear when switching fragment in navigation drawer

676 views Asked by At

I have a problem when using a navigation drawer and displaying a ListView inside it. When the user clicks on an item in the navigation drawer, it switches between the fragments to display the requested one. In one Fragment I have a ListView, and I noticed when I switch to another fragment and back to the one containing the ListView, all the Items disappeared. I also noticed, that the SwipeRefreshLayout stops working. How can I fix that?

The navigation drawer I'm using:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include android:id="@+id/toolbar" layout="@layout/toolbar" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ListView
            android:id="@+id/lv_drawer_menu"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:background="#fff"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:drawSelectorOnTop="true"/>

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

I am using this code to switch between the fragments:

private void setFragment(int position, Class<? extends Fragment> fragmentClass) {
        invalidateOptionsMenu();
        try {
            Fragment fragment = fragmentClass.newInstance();
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frame_container, fragment, fragmentClass.getSimpleName());
            fragmentTransaction.commit();

            toolbar.setTitle(fragmentTitles[position]);
            drawerMenu.setItemChecked(position, true);
            drawerLayout.closeDrawer(drawerMenu);
            drawerMenu.invalidateViews();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
0

There are 0 answers