2017-06-20 3 views
2

Donc, mon problème est ceci, je veux changer la mise en page quand un bouton différent est cliqué dans la barre du bas.
Je souhaite afficher un widget différent lorsque je clique sur chaque bouton. Mon activity_main.xml est la suivante:Modifier FrameLayout dynamiquement dans un BottomNavigationView

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

<LinearLayout 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" 
    android:orientation="vertical" 
    tools:context="com.codanimex.android.prova.MainActivity"> 

    <FrameLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <TextView 
      android:id="@+id/message" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/activity_vertical_margin" 
      android:layout_marginLeft="@dimen/activity_horizontal_margin" 
      android:layout_marginRight="@dimen/activity_horizontal_margin" 
      android:layout_marginTop="@dimen/activity_vertical_margin" 
      android:text="@string/title_home" /> 

    </FrameLayout> 

    <android.support.design.widget.BottomNavigationView 
     android:id="@+id/navigation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:background="?android:attr/windowBackground" 
     app:menu="@menu/navigation" /> 

</LinearLayout> 

Mon MainActivity.java est:

package com.codanimex.android.prova; 

import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.design.widget.BottomNavigationView; 
import android.support.v7.app.AppCompatActivity; 
import android.view.MenuItem; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    private TextView mTextMessage; 

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

     @Override 
     public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
      switch (item.getItemId()) { 
       case R.id.navigation_home: 
        mTextMessage.setText(R.string.title_home); 
        return true; 
       case R.id.navigation_dashboard: 
        mTextMessage.setText(R.string.title_dashboard); 
        return true; 
       case R.id.navigation_notifications: 
        mTextMessage.setText(R.string.title_notifications); 
        return true; 
      } 
      return false; 
     } 

    }; 

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

     mTextMessage = (TextView) findViewById(R.id.message); 
     BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 
     navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 
    } 

} 

Si je dois utiliser des fragments, pouvez-vous l'expliquer? Je ne pouvais pas comprendre leur fonctionnement.
Merci pour votre aide.

Répondre

1

Tout d'abord, vous créez Fragment pour chaque bouton, avec sa propre mise en page Par exemple: HomeFragment, DashboardFragment, NotificationFragment.

puis supprimez le TextView de l'intérieur de la FrameLayout, car ce n'est plus nécessaire, puisque vous allez montrer le fragment lui-même. activity_main.xml sera:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
    android:orientation="vertical" 
    tools:context="com.codanimex.android.prova.MainActivity"> 

    <FrameLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    </FrameLayout> 

    <android.support.design.widget.BottomNavigationView 
     android:id="@+id/navigation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:background="?android:attr/windowBackground" 
     app:menu="@menu/navigation" /> 

</LinearLayout> 

Ensuite Mise à jour le code MainActivity.java pour basculer entre les fragments:

public class MainActivity extends AppCompatActivity { 

    private enum NavigationFragment{ 
     Home, 
     Dashboard, 
     Notification 
    } 

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

     @Override 
     public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
      switch (item.getItemId()) { 
       case R.id.navigation_home: 
        ChangeFragment(NavigationFragment.Home); 
        return true; 
       case R.id.navigation_dashboard: 
        ChangeFragment(NavigationFragment.Dashboard); 
        return true; 
       case R.id.navigation_notifications: 
        ChangeFragment(NavigationFragment.Notification); 
        return true; 
      } 
      return false; 
     } 

    }; 

    private void ChangeFragment(NavigationFragment value){ 
     Fragment fragment = null; 
     switch (value) { 
      case Home: fragment = new HomeFragment(); break; 
      case Dashboard: fragment = new DashboardFragment(); break; 
      case Notification: fragment= new NotificationFragment();break; 
     } 
     if(fragment!=null) 
      getSupportFragmentManager() 
        .beginTransaction() 
        .replace(R.id.content, fragment) 
        .commit(); 

    } 


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

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

} 
+0

si vous essayez seulement de remplacer un contenu statique avec un autre contenu statique qui ne le font pas avoir une action, alors vous devriez envisager de remplacer le concept de Fragment par un remplacement normal de la vue 'frameLayout.removeAllViews(); frameLayout.addView (voir); ' – omarmallat