从MaterialDrawer中选择时,片段未加载

时间:2017-03-06 15:37:31

标签: android xml android-layout android-fragments materialdrawer

我有一个创建了MaterialDrawer的活动。材料抽屉代码如下

    SecondaryDrawerItem item1 = new SecondaryDrawerItem().withIdentifier(1).withName(R.string.drawer_item_home)
            .withIcon(new IconicsDrawable(this)
                    .icon(GoogleMaterial.Icon.gmd_home)
                    .sizeRes(R.dimen.activity_horizontal_margin));
    SecondaryDrawerItem item2 = new SecondaryDrawerItem().withIdentifier(2).withName(R.string.drawer_item_graph)
            .withIcon(new IconicsDrawable(this)
                    .icon(GoogleMaterial.Icon.gmd_trending_up)
                    .sizeRes(R.dimen.activity_horizontal_margin));
    SecondaryDrawerItem item3 = new SecondaryDrawerItem().withIdentifier(3).withName(R.string.drawer_item_map)
            .withIcon(new IconicsDrawable(this)
                    .icon(GoogleMaterial.Icon.gmd_map)
                    .sizeRes(R.dimen.activity_horizontal_margin));
    SecondaryDrawerItem item4 = new SecondaryDrawerItem().withIdentifier(4).withName(R.string.drawer_item_settings)
            .withIcon(new IconicsDrawable(this)
                    .icon(GoogleMaterial.Icon.gmd_settings)
                    .sizeRes(R.dimen.activity_horizontal_margin))
            .withSelectable(false);
    new DrawerBuilder()
            .withActivity(this)
            .withToolbar(getToolbar())
            .withSelectedItem(1)
            .withAccountHeader(headerResult)
            .withActionBarDrawerToggleAnimated(true)
            .addDrawerItems(
                    item1,
                    item2,
                    item3,
                    new DividerDrawerItem(),
                    item4
            )
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                    // do something with the clicked item :D
                    if (drawerItem != null) {
                        if (drawerItem.getIdentifier() == 1) {
                            getSupportFragmentManager().beginTransaction()
                                    .replace(R.id.fragment, new WeatherFragment())
                                    .commit();
                        }
                        else if (drawerItem.getIdentifier() == 2) {
                            getSupportFragmentManager().beginTransaction()
                                    .replace(R.id.fragment, new GraphsFragment())
                                    .commit();
                        }
                        else if (drawerItem.getIdentifier() == 3) {
                            getSupportFragmentManager().beginTransaction()
                                    .replace(R.id.fragment, new MapsFragment())
                                    .commit();
                        }
                        else if (drawerItem.getIdentifier() == 4) {
                            startActivity(new Intent(WeatherActivity.this, AboutActivity.class));
                        }
                    }
                    return false;
                }
            })
            .build();

当我的应用启动时,所选的默认选项为1,即。应用程序启动时,活动会自动加载WeatherFragment。单击列表中的第二项时出现问题:Graphs Fragment。根据代码,它应该有效,但它的作用是保留视图中的旧片段,并且不加载新片段的布局。

这是活动XML布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:context="com.a5corp.weather.activity.WeatherActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.NoActionBar.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.NoActionBar.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_weather" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@drawable/ic_search_white_24dp" />

</android.support.design.widget.CoordinatorLayout>

content_weather.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.a5corp.weather.fragment.WeatherFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_weather" />

到目前为止,显而易见的是WeatherActivity将始终加载fragment_weather(WeatherFragment)。但是,当我单击GraphsFragment(第二项)时,它不会加载该布局

这是我的GraphsFragment代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Log.i("Loaded" , "Fragment");     //This Log info shows up in the Android Logger
    rootView = inflater.inflate(R.layout.fragment_graphs, container, false);

    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MaterialDialog.Builder builder = new MaterialDialog.Builder(this.getActivity())
            .title("Please Wait")
            .content("Loading")
            .progress(true , 0);
    builder.build().show();
}

fragment_graphs.xml:

<android.support.v4.widget.SwipeRefreshLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="111dp"
        android:text="Button"/>

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginTop="40dp"
        android:text="Switch"/>

</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

进度对话框显示如此,但是没有加载像GraphsFragment XML中的按钮和开关等布局项。

如何在从抽屉中单击时加载GraphsFragment,同时显示布局膨胀和显示的进度对话框

1 个答案:

答案 0 :(得分:0)

原来我只需要添加FrameLayout而不是include布局。

这是修改后的主XML文件

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context="com.a5corp.weather.activity.WeatherActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/appBar"
        android:theme="@style/AppTheme.NoActionBar.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.NoActionBar.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/fragment"
        android:paddingTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.a5corp.weather.WeatherActivity"
        tools:ignore="MergeRootFrame"
        android:background="@color/colorPrimary" />

</android.support.design.widget.CoordinatorLayout>