Estoy teniendo un error de superposición de fragments cuando intento cambiarlos programáticamente.
Adjunto imágen donde se puede ver que hay un fragment por encima de otro (aclaro que el fragment que está por encima que es un chat, puede ser usado pero el de abajo solo está como fondo y no es funcional)
Adjunto el código de la actividad que contiene el navigation drawer, el cual es el responsable de detectar la selección de los nav_items
NavigationView nv = findViewById(R.id.nav_view);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem){
int menu_id = menuItem.getItemId();
Fragment fragment = null;
FragmentManager fragmentManager = getSupportFragmentManager();
switch(menu_id) {
case R.id.nav_inicio:
fragment = new fragment_inicio();
Toast.makeText(MainActivity.this, "inicio", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_perfil:
fragment = new fragment_perfil();
Toast.makeText(MainActivity.this, "PERFIL", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_chat:
fragment = new fragment_chat();
Toast.makeText(MainActivity.this, "CHAT", Toast.LENGTH_SHORT).show();
}
fragmentManager.beginTransaction().replace(R.id.nav_host_fragment, fragment).commit();
return false;
}
});
Adjunto el código XML de content_main (layout que crea Android Studio por defecto al crear una app de tipo Navigation Drawer)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
android:contentDescription="@string/ayuda">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation"
android:contentDescription="@string/ayuda"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Edit: Código java en el cual obtengo error dentro del MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = findViewById(R.id.drawer_layout);
nv = findViewById(R.id.nav_view);
nombree = findViewById(R.id.et_ciudad);
abc = new AppBarConfiguration.Builder(R.id.nav_inicio, R.id.nav_perfil,
R.id.nav_chat, R.id.nav_buscar, R.id.nav_mis_clases, R.id.nav_ayuda,
R.id.nav_configuracion)
.setDrawerLayout(drawer).build();
//LÍNEA EN LA CUAL OBTENGO ERROR
nc = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(nv, nc);
nv.setCheckedItem(R.id.nav_inicio);
nv.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
.
.
.}
Gracias por leer