When starting the fragment and loading the recyclerview, it duplicates the items.
private void loadData() {
if (categoriaList.size() > 0)
categoriaList.clear();
miprogress.setVisibility(View.VISIBLE);
db.collection("categoria")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (DocumentSnapshot doc : task.getResult()) {
CategoriaModel cat = new CategoriaModel(doc.getString("nombre"), doc.getString("url"), doc.getId());
categoriaList.add(cat);
}
adapter = new CategoriaAdapter((MainActivity) getActivity(), categoriaList);
listItem.setAdapter(adapter);
miprogress.setVisibility(View.GONE);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
In my onresume and onactivity created I have it this way:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listItem = getActivity().findViewById(R.id.recycler_categorias);
miprogress = getView().findViewById(R.id.circularProgress);
layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
listItem.setHasFixedSize(true);
listItem.setLayoutManager(layoutManager);
}
@Override
public void onResume() {
super.onResume();
loadData();
}
The funny thing here is that when I change Activity or another fragment, the list does not duplicate and it looks good. I do not know why it only happens when you start the application