我有一个创建了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,即。Activity 会在 App 启动时自动加载 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();
}
片段图.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,以及膨胀的布局和显示的进度对话框