I am working an Android application that has: Bottom Navigation Firebase Fragments RecyclerView In a fragment I make a firebase query in real time and with the data full a recyclerView, at first everything works fine but I click again on the option that calls the fragment again, it turns white it is as if it could not be loaded new the fragment to the designated container.
Navigation container code and fragments:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Inicio");
setContentView(R.layout.activity_contenedor_deudor);
getSupportFragmentManager().beginTransaction().replace(R.id.contenedor,new FragmentoInicio()).commit();
nav_bottom=(BottomNavigationView) findViewById(R.id.navigationBottom);
removeShiftMode(nav_bottom);
nav_bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_bottom_inicio:
closeOptionsMenu();
getSupportFragmentManager().beginTransaction().replace(R.id.contenedor, new FragmentoInicio()).commit();
break;
case R.id.nav_bottom_deudores:
getSupportFragmentManager().beginTransaction().replace(R.id.contenedor, new FragmentoDeudores()).commit();
break;
case R.id.nav_bottom_historial:
setTitle("Historial de Visitas");
getSupportFragmentManager().beginTransaction().replace(R.id.contenedor, new FragmentoHistorial()).commit();
break;
case R.id.nav_bottom_configuracion:
setTitle("Configuración de Usuario");
getSupportFragmentManager().beginTransaction().replace(R.id.contenedor, new FragmentoConfiguracion()).commit();
break;
}
return true;
}
});
}
@Override
public void onBackPressed() {
finishAfterTransition();
}
private void removeShiftMode(BottomNavigationView bottomNavigationView) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.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);
//noinspection RestrictedApi
item.setShiftingMode(false);
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
}
And this is the code of the fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_fragmento_deudores, container, false);
listaDeudores=new ArrayList<>();
recyclerView = (RecyclerView)v.findViewById(R.id.recyclerdeudores);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adaptadorDeudor = new AdaptadorDeudor(listaDeudores, getActivity());
adaptadorDeudor.setOnAccionesDeduores(new AccionesDeudores() {
@Override
public void onActividadDetalle(int position) {
Intent intent=new Intent(getActivity(), DetalleContribuyente.class);
Deudor duedorPresionado=listaDeudores.get(position);
intent.putExtra("deudor",duedorPresionado);
startActivity(intent);
}
});
recyclerView.setAdapter(adaptadorDeudor);
DatabaseReference database= FirebaseDatabase.getInstance().getReference();
database.child("deudor").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listaDeudores.removeAll(listaDeudores);
for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren()
) {
Deudor deudor=dataSnapshot1.getValue(Deudor.class);
listaDeudores.add(deudor);
}
adaptadorDeudor.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return v;
}
public ArrayList<Deudor> obtenerLista(){
listaDeudores=new ArrayList<>();
DatabaseReference database= FirebaseDatabase.getInstance().getReference();
database.child("deudor").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listaDeudores.removeAll(listaDeudores);
for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren()
) {
Deudor deudor=dataSnapshot1.getValue(Deudor.class);
listaDeudores.add(deudor);
}
adaptadorDeudor.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return listaDeudores;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
try {
ArrayList<Deudor> listaFiltrada=filtrador(listaDeudores,newText);
adaptadorDeudor.setFilter(listaFiltrada);
//adaptadorDeudor=new AdaptadorDeudor(listaFiltrada);
//recyclerView.setAdapter(adaptadorDeudor);
}catch (Exception e){
}
return false;
}
private ArrayList<Deudor> filtrador(ArrayList<Deudor> arreglo, String texto){
ArrayList<Deudor> listaFiltrada=new ArrayList<Deudor>();
try{
texto=texto.toLowerCase();
for(Deudor item:arreglo){
String nombre=item.getNombre().toLowerCase();
String numero=item.getNumero().toLowerCase();
String apellido=item.getApellido().toLowerCase();
if(nombre.contains(texto) || numero.contains(texto) || apellido.contains(texto)){
listaFiltrada.add(item);
}
}
}catch(Exception e){
}
return listaFiltrada;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.buscar:
break;
case R.id.nav_detalle_contribuyente_actualizar:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_contenedor_deudor,menu);
MenuItem item=menu.findItem(R.id.buscar);
SearchView searchView=(SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adaptadorDeudor.setFilter(listaDeudores);
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
I hope you can help me, the problem is when I return to press the "debtors button" being already in that fragment, thank you very much.