0

我有一個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,即。該活動會在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(); 
} 

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切換的佈局項目不會加載。

我將如何從抽屜中單擊時,隨着充氣的佈局和顯示

+1

的問題是無關的MaterialDrawer本身。這似乎是由最初的Fragment如何應用的方式造成的。你可以嘗試創建一個FrameLayout來代替那裏的片段定義,然後將片段應用到這個? (你可以啓用在初始創建時也觸發onClickListener(因此所有的片段處理可以通過抽屜發生) – mikepenz

+0

嘿,先生,謝謝,你是傳奇人物!原來我沒有使用FrameLayout導致了這個問題。 – Sparker0i

回答

0

進度對話框一起加載GraphsFragment原來我只需要添加的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>