Nous avons des onglets de navigation en bas (4) et chaque onglet contient des fragments. Quelqu'un pourrait-il aider (donner une idée) comment concevoir la structure de manière MVVM en conservant l'état du fragment pour chaque onglet. Je sais que ce n'est pas l'endroit pour les mauvaises questions mais je suis à la recherche de conseils conceptuels. Pour l'accomplir de la meilleure façon possible.BottomNavigationView avec des fragments de manière MVVM
-2
A
Répondre
0
Google (par le biais de Nick Butcher) a annoncé la sortie de la version 25 de la bibliothèque Android Design Support Library, qui inclut le nouveau BottomNavigationView.
menu.xml
Définir les éléments de navigation (Fragments éléments) dans le fichier de ressources de menu
<?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
AJOUTER la BottomNavigationView
réelle à la mise en page .
<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();
}
Cette classe d'aide et d'animation Supprimer Déplacement
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");
}
}
}
Chaque fragment comme un élément de menu**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);
}
}
Merci pour l'effort Harsh mais qu'en est-il de l'état du fragment conservateur. – Medet
Pouvez-vous spécifier l'état du fragment? ce que tu veux exactement. –
Je veux garder tous les 4 fragments en mémoire une fois créés pour éviter la recréation de fragments chaque fois que l'utilisateur bascule entre les onglets – Medet