Tengo un problema en Android, estoy haciendo uso de un Bottom navigation, el problema es que cuando cambio entre fragmentos si hace el cambio pero la vista pero la vista del primer fragmento se sigue viendo en los demás aun que se cambie.
Lo que esta dentro del cuadro azul es la vista de primer fragmento que seria el home, y lo que esta en rojo es la vista del segundo fragmento que es el dashboard.
El fragmento home tiene un recycler view y cards view.
Este es el XML del MainActivity, aqui esta el nav_host_fragment donde hago la transaccion:
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" >
</com.google.android.material.bottomnavigation.BottomNavigationView>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="43dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
Esta es la manera en la que manejo las transacciones de los fragmentos.
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
final Fragment homeFragment = new HomeFragment();
final Fragment linksFragment = new DashboardFragment();
final Fragment quienesSomos = new NotificationsFragment();
final FragmentManager fragmentManager = getSupportFragmentManager();
Fragment active = homeFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations
navView.setOnNavigationItemSelectedListener(myOnNavigationListener);
fragmentManager.beginTransaction().add(R.id.nav_host_fragment, quienesSomos, "quienesSomos").hide(quienesSomos).commit();
fragmentManager.beginTransaction().add(R.id.nav_host_fragment, linksFragment, "linksFragment").hide(linksFragment).commit();
fragmentManager.beginTransaction().add(R.id.nav_host_fragment, homeFragment, "homeFragment").hide(homeFragment).commit();
Log.d("ACTIVE","Activo: "+active.getTag());
}
private BottomNavigationView.OnNavigationItemSelectedListener myOnNavigationListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_home:
fragmentManager.beginTransaction().hide(active).show(homeFragment).commit();
//fragmentManager.beginTransaction().replace(active.getId(), homeFragment).commit();
active = homeFragment;
Log.d("ACTIVE","Activo: "+active.getTag());
return true;
case R.id.navigation_links:
fragmentManager.beginTransaction().hide(active).show(linksFragment).commit();
//fragmentManager.beginTransaction().replace(active.getId(), linksFragment).commit();
active = linksFragment;
Log.d("ACTIVE","Activo: "+active.getTag());
return true;
case R.id.navigation_qsomos:
fragmentManager.beginTransaction().hide(active).show(quienesSomos).commit();
//fragmentManager.beginTransaction().replace(active.getId(), quienesSomos).commit();
active = quienesSomos;
Log.d("ACTIVE","Activo: "+active.getTag());
return true;
}
return false;
}
};
}
No se por que no se quita el primer fragmento que se muestra al cambiar a otros fragmentos.
Este es el XML del fragment home, donde se carga mi ListView:
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/list"
android:name="com.seccion.seccion15.ui_session.inicio_session.InicioSessionItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.home.HomeFragment"
tools:listitem="@layout/fragment_home_item_list" />
Y este es donde cargo mi CardView que se mostrara en la lista:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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="120dp"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/titulo_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="Titlo de la noticia"
android:textColor="#000000"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imagen_noticia"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/vista_previa"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:maxLines="3"
android:text="Vista previa"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imagen_noticia"
app:layout_constraintTop_toBottomOf="@+id/titulo_post" />
<TextView
android:id="@+id/fecha_post"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="15dp"
android:text="Fecha del post"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imagen_noticia"
app:layout_constraintTop_toBottomOf="@+id/vista_previa" />
<ImageView
android:id="@+id/imagen_noticia"
android:layout_width="102dp"
android:layout_height="95dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>