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です。アクティビティは、アプリケーションの起動時に自動的にWeatherFragmentをロードします。この問題は、リストの2番目の項目、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は常に(WeatherFragment)をfragment_weatherロードすることです。しかし、私はGraphsFragment(2番目の項目)をクリックしたとき、それはそのレイアウトをロードしません。ここ

は私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自体には無関係です。これは、最初のフラグメントがどのように適用されるかによって引き起こされるようです。そこにあるフラグメント定義の代わりにFrameLayoutを作成して、これにフラグメントを適用することはできますか? (あなたは、最初の作成時にもonClickListenerが起動されるようにすることができます(したがって、すべてのフラグメントハンドリングは引き出しを介して行われる可能性があります) – mikepenz

+0

こんにちは、伝説です! – Sparker0i

答えて

0

示す進捗ダイアログと一緒に、引き出しからクリックされたとき、私はGraphsFragmentをロードする方法

は、私が唯一のレイアウトをINGの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> 
関連する問題