2016-03-24 10 views
-1

Ich bin vor ziemlich schwer fragments in der Navigation zu implementieren. Sobald ein Benutzer auf das Objekt klickt, das ich anzeigen möchte, wird das Fragment in FrameLayout angezeigt. Aber wenn ich das tue, zeigt es mir den Inhalt der Elternaktivität sogar Fragment wird in FrameLayout ersetzt.Wie mit Fragmenten in Navigationsleiste arbeitet

Dies ist die xml von Eltern Aktivität

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/container" 
    tools:context="com.softoven.ultron.HomeActivity"> 
    <include 
     android:id="@+id/toolbar_layout" 
     layout="@layout/toolbar" /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="10dp" 
     android:paddingTop="10dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:background="@drawable/background_shadow" 
     android:orientation="horizontal" 
     android:id="@+id/linearLayout"> 
     <CheckedTextView 
      android:layout_marginLeft="10dp" 
      android:textSize="14sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:clickable="true" 
      android:id="@+id/latest" 
      android:textColor="@color/text_color" 
      android:text="LATEST" /> 
     <CheckedTextView 
      android:textSize="14sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:clickable="true" 
      android:id="@+id/world" 
      android:textColor="@color/text_color" 

      android:text="WORLD" /> 
     <CheckedTextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:clickable="true" 
      android:id="@+id/national" 
      android:textColor="@color/text_color" 
      android:text="NATIONAL" 
      android:textSize="14sp" 
      /> 
     <CheckedTextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:clickable="true" 
      android:id="@+id/business" 
      android:text="BUSINESS" 
      android:textColor="@color/text_color" 
      android:textSize="14sp" 
      /> 
     <CheckedTextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:clickable="true" 
      android:id="@+id/finance" 
      android:text="FINANCE" 
      android:textColor="@color/text_color" 
      android:textSize="14sp" 
      /> 
    </LinearLayout> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/content_container" 
     android:layout_below="@+id/toolbar_layout" 
     android:layout_above="@+id/linearLayout" 
     /> 
    <!--these shouldn't cause any problem now since they aren't chlid of FrameLayout--> 
    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/listView" 
     android:layout_below="@+id/toolbar_layout" 
     android:layout_above="@+id/linearLayout" 
     /> 

     <ProgressBar 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/progressBar" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" 
      /> 

</RelativeLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#000000" 
    app:menu="@menu/menu" 
    app:itemTextColor="#ffffff" 
    app:itemIconTint="#ffffff"/> 

und das ist xml von Fragment

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.softoven.ultron.activities.BookmarkActivity"> 
    <ListView 
     android:layout_marginTop="5dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/bookmarks"> 
    </ListView> 
</RelativeLayout> 

hier ist der Code, den ich für verwenden Fragment tra nsaction.

@Override 
      public boolean onNavigationItemSelected(MenuItem item) { 
       int id = item.getItemId(); 
       item.setChecked(true); 
       drawerLayout.closeDrawers(); 
       switch (id) { 
        case R.id.news: 
         Toast.makeText(HomeActivity.this, "News", Toast.LENGTH_LONG).show(); 
         return true; 
        case R.id.bookmarks: 
         FragmentManager fm = getSupportFragmentManager(); 
         fm.beginTransaction().replace(R.id.content_container,new BookmarkFragment()).commit(); 
         return true; 
       } 
       return true; 
      } 

Einige Hilfe würde geschätzt werden. Vielen Dank!

+0

'FragmentTransaction's nur Handle' Fragment's. Sie ersetzen/entfernen keine 'View's nicht in einem' Fragment'. Wenn Sie möchten, dass die Views im FrameLayout entfernt werden, müssen Sie dies selbst tun oder sie in ein Fragment setzen, das Sie beim Start laden. In diesem Fall funktionieren die Transaktionen wie erwartet . –

+0

überprüfen Sie diesen Link http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ –

+0

ich die Frage aktualisiert haben. schaue bitte hinein. Jetzt habe ich kein Kind zu framelayout, aber immer noch bekomme ich das gleiche Ergebnis –

Antwort

1

Das Problem FrameLayout ist hat Views als Kinder. Die Ersetzungsmethode ersetzt nur vorherige Fragmente, wenn in den FrameLayout NICHT die Ansichten hinzugefügt werden.

Um dies zu lösen, könnten Sie die Kinder aus dem FrameLayout entfernen und es zu einem separaten Fragment hinzufügen. Fügen Sie dann das Fragment in dem FrameLayout als sein einziges Kind in Ihrem XML hinzu.

Dann wenn Sie replace die Fragment wird es schließlich ersetzt werden.

+0

Ok jetzt habe ich die Ansichten von Framelayout entfernt. und ich bekomme das gleiche Ergebnis –

+0

Bitte posten Sie Ihr aktuelles Layout. –

+0

Ich habe aktualisiert xml goodly Blick hinein –

0

versuchen diese

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/listView" 
    /> 

    <ProgressBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/progressBar" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="center" /> 
</FrameLayout> 
Verwandte Themen