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
-2
A
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ächlichenBottomNavigationView
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);
}
}
Verwandte Themen
- 1. Getrennter Backstack für jede Registerkarte in BottomNavigationView Android mit Fragmenten
- 2. BottomNavigationView mit Aktivitäten
- 3. Verwenden von Aktivitäten mit BottomNavigationView
- 4. Unten Navigationsleiste mit Fragmenten
- 5. Margen Navigationspunkt in BottomNavigationView
- 6. Feste untere Navigationsleiste mit BottomNavigationView
- 7. BottomNavigationView mit FloatingActionButton in CoordinatorLayout nicht Position
- 8. BottomNavigationView disable Schaltmodus ohne Reflexion mit
- 9. BottomNavigationView - Wie ändert man die Menüelementschrift?
- 10. Fragestatus bei Verwendung von Android speichern BottomNavigationView
- 11. Bearbeiten von Datensätzen mit MVVM/MVVM-Light
- 12. BottomNavigationView überlappende FrameLayout in CoordinatorLayout
- 13. ViewPager mit Fragmenten - onPause(), onResume()?
- 14. Android Toolbar mit Fragmenten
- 15. Layout-Setup mit Fragmenten
- 16. Zwei aktive Farben in BottomNavigationView
- 17. Android-Schublade mit Fragmenten
- 18. AppBarLayout mit Fragmenten
- 19. FindViewbyID mit Fragmenten
- 20. Kommunikation zwischen Fragmenten über Schnittstellen
- 21. Listenadapter mit Fragmenten
- 22. SearchView mit Fragmenten
- 23. ViewPager MVVM, DataBinding Android
- 24. Spinner mit Fragmenten
- 25. Die Sortierung in der App mit Fragmenten
- 26. WPF: Binding TreeView in MVVM Weg Schritt für Schritt Tutorial
- 27. Wie mit Fragmenten mit Bluetooth
- 28. Kommunikation mit Fragmenten mit FragmentTransaction
- 29. Der beste Weg, um zwischen zwei Fragmenten zu wechseln
- 30. MVVM Muster mit PySide
Danke für die Mühe Harsh aber was ist mit dem Zustand des Fragments zu halten. – Medet
Können Sie den Fragmentstatus angeben? Was genau willst du? –
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