So habe ich heute ein kleines Problem gefunden. Ich habe eine untere Navigationsansicht in meiner App erstellt und nachdem ich auf Schaltflächen geklickt habe, wird das Fragment auf dem Bildschirm ersetzt (und es funktioniert einwandfrei!). Aber direkt nach dem Start der App, und ohne auf eine Schaltfläche zu klicken, ist kein Fragment auf dem Bildschirm. Ich habe festgestellt, dass die Fragmente nur nach dem Klicken auf eine Schaltfläche angezeigt werden, und ich möchte ein Standardfragment (KalulatorFragment) haben. Ich habe mein Bestes versucht, es irgendwie einzurichten, aber kein Erfolg ...Wie wird ein Standardfragment in der Hauptaktivität festgelegt?
public class Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
kalkulatorFragment kalkulator_fragment = new kalkulatorFragment();
wzoryFragment wzory_fragment = new wzoryFragment();
definicjeFragment definicje_fragment = new definicjeFragment();
switch (item.getItemId()) {
case R.id.kalkulator:
ft.replace(android.R.id.content, kalkulator_fragment);
ft.commit();
return true;
case R.id.wzory:
ft.replace(android.R.id.content, wzory_fragment);
ft.commit();
return true;
case R.id.definicje:
ft.replace(android.R.id.content, definicje_fragment);
ft.commit();
return true;
}
return false;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:design="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/definicjeFragment"
android:name="com.thegodofcoding.adamgreloch.ultracalcalpha.definicjeFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/definicje_view" />
<fragment
android:id="@+id/wzoryFragment"
android:name="com.thegodofcoding.adamgreloch.ultracalcalpha.wzoryFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/wzory_view" />
<fragment
android:id="@+id/kalkulatorFragment"
android:name="com.thegodofcoding.adamgreloch.ultracalcalpha.kalkulatorFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/kalkulator_view" />
</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="@color/bottomNavBar"
android:layout_alignParentBottom="true"
design:menu="@menu/bottom_nav_items" />
Thanks :)