2017-08-25 4 views
-2

Wir haben unteren Navigationsregisterkarten (4) und jede Registerkarte hat Fragment in es. Könnte jemand helfen (Idee zu geben), wie man die Struktur in MVVM Weise mit dem Halten des Fragmentstatus für jedes Tab gestaltet. Ich weiß, dass dies nicht der Ort für schlechte Fragen ist, aber ich suche nach einem konzeptionellen Rat. Um es auf bestmögliche Weise zu erreichen.BottomNavigationView mit Fragmenten in MVVM Weg

Antwort

0

Google (über Nick Butcher‏) kündigte die Veröffentlichung des v25 der Android Design Support Library an, die das neue BottomNavigationView enthält.

menu.xml

Navigationselemente (Fragmente Artikel) in der

die das Layout tatsächlichen BottomNavigationView ADD

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 
<item 
    android:id="@+id/action_item1" 
    android:icon="@drawable/icon1" 
    android:title="Menu1" 
/> 
<item 

    android:id="@+id/action_item2" 
    android:icon="@drawable/icon2" 
    android:title="Menu2"/> 
<item 
    android:id="@+id/action_item3" 
    android:icon="@drawable/icon3" 
    android:title="Menu3" /> 
<item 
    android:id="@+id/action_item4" 
    android:icon="@drawable/icon4" 
    android:title="Menu4" /> 

</menu> 

activity_main.xml

Datei Menüressource definieren .

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:local="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:minHeight="?attr/actionBarSize" 
     android:background="@color/colorPrimary" 
     local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     local:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 


    <FrameLayout 
     android:id="@+id/frame_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/navigation" 
     android:layout_below="@id/toolbar" 
     android:animateLayoutChanges="true"> 

    </FrameLayout> 

    <android.support.design.widget.BottomNavigationView 
     android:id="@+id/navigation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/rbtn_selector" 
     android:paddingTop="@dimen/_2sdp" 
     app:itemIconTint="@drawable/selector_bottom" 
     app:itemTextColor="@drawable/selector_bottom" 
     app:elevation="@dimen/_8sdp" 
     app:menu="@menu/menu"/> 


</RelativeLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

     bottomNavigationView = (BottomNavigationView)findViewById(R.id.navigation); 

    //If you want to remove slide animation of bottomview with Helper Class 

BottomNavigationViewHelper.removeShiftMode(bottomNavigationView); 
     Menu m = bottomNavigationView.getMenu(); 


bottomNavigationView.setOnNavigationItemSelectedListener 
      (new BottomNavigationView.OnNavigationItemSelectedListener() { 
       @Override 
       public boolean onNavigationItemSelected(@NonNull MenuItem item) { 


        if (getSupportActionBar() != null){ 
         getSupportActionBar().setDisplayHomeAsUpEnabled(false); 
         getSupportActionBar().setHomeButtonEnabled(false); 
        } 



        android.app.Fragment selectedFragment = null; 
        switch (item.getItemId()) { 
         case R.id.action_item1: 
          selectedFragment = Fragment1.newInstance(); 
          toolbar.setTitle("Fragment1"); 
          break; 
         case R.id.action_item2: 
          selectedFragment = Fragment2.newInstance(); 
          toolbar.setTitle("Fragment2"); 
          break; 
         case R.id.action_item3: 
          selectedFragment = Fragment3.newInstance(); 
          toolbar.setTitle("Fragment3"); 
          break; 
         case R.id.action_item4: 
          selectedFragment = Fragment4.newInstance(); 
          toolbar.setTitle("Fragment4"); 
          break; 
         default: 
          selectedFragment = Fragment1.newInstance(); 
          toolbar.setTitle("Fragment1"); 
          break; 

        } 
        android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
        transaction.replace(R.id.frame_layout, selectedFragment); 
        transaction.commit(); 
        return true; 
       } 
      }); 

    //Manually displaying the first fragment - one time only 



    android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
    transaction.replace(R.id.frame_layout, Fragment1.newInstance()); 
    transaction.commit(); 

    } 

Diese Hilfsklasse für Remove Shifting Animation

static class BottomNavigationViewHelper { 

    public static void removeShiftMode(BottomNavigationView view) { 
     BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0); 
     try { 
      Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode"); 
      shiftingMode.setAccessible(true); 
      shiftingMode.setBoolean(menuView, false); 
      shiftingMode.setAccessible(false); 
      for (int i = 0; i < menuView.getChildCount(); i++) { 
       BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i); 
      item.setShiftingMode(false); 
       // set once again checked value, so view will be updated 
       item.setChecked(item.getItemData().isChecked()); 
      } 
     } catch (NoSuchFieldException e) { 
      Log.e("ERROR NO SUCH FIELD", "Unable to get shift mode field"); 
     } catch (IllegalAccessException e) { 
      Log.e("ERROR ILLEGAL ALG", "Unable to change value of shift mode"); 
     } 
    } 
} 

Jedes Fragment als Menüpunkt**Fragmen1.java**

public class Fragmen1 extends android.app.Fragment { 

public static Fragmen1 newInstance() { 
    Fragmen1 fragment = new Fragmen1(); 
    return fragment; 
} 
    @Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 
} 

} 
+0

Danke für die Mühe Harsh aber was ist mit dem Zustand des Fragments zu halten. – Medet

+0

Können Sie den Fragmentstatus angeben? Was genau willst du? –

+0

Ich möchte alle 4 Fragmente im Speicher behalten, sobald sie erstellt wurden, um die Wiederherstellung von Fragmenten jedes Mal zu vermeiden, wenn der Benutzer zwischen den Tabs wechselt – Medet

Verwandte Themen