2017-08-25 46 views
-2

我們有底部導航選項卡(4),每個選項卡都有片段。任何人都可以幫助(給出想法)如何以MVVM方式設計結構,並保留每個選項卡的片段狀態。我知道這不是問題的地方,但我正在尋找概念性建議。以最好的方式完成它。BottomNavigationView帶有MVVM方式的片段

回答

0

Google(通過Nick Butcher‏)宣佈發佈包含新BottomNavigationView的Android設計支持庫v25。

menu.xml文件

定義菜單中的資源文件

<?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中

導航項目(片段項目)添加實際BottomNavigationView佈局。

<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(); 

    } 

這個輔助類刪除移動畫

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"); 
     } 
    } 
} 

各片段作爲菜單項**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

感謝努力苛刻,但保持碎片的狀態如何。 – Medet

+0

你能指定片段狀態嗎?你想要什麼。 –

+0

我想保留所有4個片段一次創建,以避免每次用戶切換標籤時重新創建片段 – Medet