2017-12-28 37 views
3

Ich habe eine untere Navigationsleiste in meiner Hauptaktivität. Durch Klicken auf eine der Registerkarten in meiner unteren Navigation möchte ich das Fragment in der Ansicht ändern. Ich habe den folgenden Code: Hauptaktivität:Unten Navigationsleiste mit Fragmenten

public class StartActivity extends AppCompatActivity { 

SwiftFragment swiftFragment; 
FrameworksFragment frameworksFragment; 
FrameLayout content; 
android.support.v4.app.FragmentManager fragmentManager; 

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener 
     = new BottomNavigationView.OnNavigationItemSelectedListener() { 

    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
     android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction(); 

     switch (item.getItemId()) { 
      case R.id.navigation_home: 
       Log.i("Nachricht", "HOME"); 
       return true; 
      case R.id.navigation_swift: 
       Log.i("Nachricht", "SWIFT"); 
       transaction.replace(android.R.id.content, swiftFragment); 
       transaction.commit(); 
       return true; 
      case R.id.navigation_frameworks: 
       Log.i("Nachricht", "FRAMEWORKS"); 
       transaction.replace(android.R.id.content, frameworksFragment); 
       transaction.commit(); 
       return true; 
      case R.id.navigation_profile: 
       Log.i("Nachricht", "PROFILE"); 
     } 
     return false; 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_start); 

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 

    swiftFragment = (SwiftFragment) android.support.v4.app.Fragment.instantiate(this, SwiftFragment.class.getName(), null); 
    frameworksFragment = (FrameworksFragment) android.support.v4.app.Fragment.instantiate(this, FrameworksFragment.class.getName(), null); 
    content = (FrameLayout) findViewById(android.R.id.content); 
    fragmentManager = getSupportFragmentManager(); 
} 

}

Einer meiner Fragmente:

public class SwiftFragment extends Fragment { 

ArrayList<ModelTutorial> items; 
ListView list; 
ArrayAdapter adapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_swift, container, false); 

    //TODO: Get Firebase Data 
    items = new ArrayList<>(); 
    items.add(new ModelTutorial("Swift Anlegen der Liste", "TableView", "Test Test Test", null, null)); 
    items.add(new ModelTutorial("Anlegen der Liste", "TableView", "Test Test Test", null, null)); 
    items.add(new ModelTutorial("Anlegen der Liste", "TableView", "Test Test Test", null, null)); 

    list = (ListView) view.findViewById(R.id.swiftTutorialsList); 
    adapter = new ListAdapter(getActivity(), items); 
    list.setAdapter(adapter); 

    return view; 
} 

} 

Wenn ich auf eine der Registerkarten klicken, wird das rechte Fragment zeigt nach oben, so das funktioniert. Wenn jedoch das neue Fragment angezeigt wird und ich auf eine andere Registerkarte klicken möchte, um ein anderes Fragment anzuzeigen, funktioniert das nicht. Die untere Navigationsleiste reagiert nicht auf die Klicks. Selbst die Log.i Anweisungen funktionieren nicht, so scheint es, dass die OnNavigationItemSelectedListener nicht aufgerufen wird.

Ich bin komplett neu in Android-Entwicklung und ich verstehe nicht, warum das nicht funktioniert. Ich würde es schätzen, wenn jemand mir mit meinem Problem helfen könnte.

EDIT: XML-Datei StartActivity:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
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/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.marcelspindler.codingtutorials.StartActivity"> 

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/navigation" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="0dp" 
    android:layout_marginStart="0dp" 
    android:background="?android:attr/windowBackground" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:menu="@menu/navigation" /> 
</android.support.constraint.ConstraintLayout> 

XML Datei-Fragment:

<FrameLayout 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.example.marcelspindler.codingtutorials.SwiftFragment"> 

<!-- TODO: Update blank fragment layout --> 

<ListView 
    android:id="@+id/swiftTutorialsList" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</FrameLayout> 
+0

Können Sie auch eine XML-Datei hinzufügen? Überprüfen Sie, ob Ihr Listener tatsächlich angerufen wird, aber False zurückgibt. In diesem Fall wird er nicht protokolliert. –

+2

Benötigen xml-Layout und ich denke, es wäre ein Fehler in XML, weil Ihr Ersetzen von Fragment mit 'android.R.id.content' wenn Sie das tun, wird das gesamte Fenster zum Beispiel ersetzen, indem Farbe für Fragment Hintergrund setzen –

+0

Ich habe meine Frage bearbeitet und fügte die XML-Dateien hinzu. @RickSanchez Ich habe versucht, zu protokollieren, wenn es falsch zurückgibt, aber das wird nie aufgerufen. @HermanSTobi Ich habe die Hintergrundfarbe geändert und du hast Recht, sie deckt das gesamte Fenster ab, auch die untere Navigationsleiste. Kennen Sie einen Weg, das zu vermeiden, wenn 'android.R.id.content' nicht funktioniert? – Marcel

Antwort

2

Sie müssen das Fragment in einen Container in Ihrem MainActivity hinzuzufügen.

versuchen, etwas wie folgt aus:

<android.support.constraint.ConstraintLayout 
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/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.marcelspindler.codingtutorials.StartActivity"> 

<FrameLayout 
    android:id="@+id/fragment_container" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_marginEnd="0dp" 
    android:layout_marginStart="0dp" 
    android:background="?android:attr/windowBackground" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
/> 

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/navigation" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="0dp" 
    android:layout_marginStart="0dp" 
    android:background="?android:attr/windowBackground" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:menu="@menu/navigation" /> 
</android.support.constraint.ConstraintLayout> 

Und dann ersetzen Sie ersetzen Linien:

transaction.replace(R.id.fragment_container, fragment);

BONUS: Sie Ihre Fragmente wie folgt einleiten: swiftFragment = new SwiftFragment();

Werfen Sie einen Blick darauf: Best practice for instantiating a new Android Fragment

+0

funktioniert super, danke! – Marcel

Verwandte Themen