I have a problem with a SwipeRefreshLayout, I want to implement it to be able to perform the Pull to refresh function in a view that contains a RecyclerView, the problem is that when I add the SwipeRefreshLayout and run my app, the RecyclerView no longer shows me anything, it shows me completely blank, this is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
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_height="match_parent"
android:layout_width="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:name="com.seccion.yugioh.ui.inicio.InicioCartasItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.inicio.InicioCartasItemFragment"
tools:listitem="@layout/fragment_inicio_cartas_item" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
I don't understand what the problem is, I'm just adding the SwipeRefresh to be able to reload my RecyclerView when the user pulls the view but once it's added I don't see anything in the RecyclerView anymore.
The list is supposed to display a list of cards:
And when I run my application with SwipeRefresh, nothing is shown:
And this is my class where I load that RecyclerView:
public class InicioCartasItemFragment extends Fragment {
private int mColumnCount = 1;
//Declaramos como variable global el recyclerView
private RecyclerView recyclerView;
//Declaramos como variable global nuestro adaptador
private MyInicioCartasItemRecyclerViewAdapter adapter;
//Creamos una lista gobal de nuestra respuesta del servidor que contiene el modelo de nuestro json
private List<ResponseCartas> cartasList;
//Creamos una instancia a nuestra clase ViewModel que nos permitira recuperar la informacion y enviarla al adapter
private CartasViewModel cartasViewModel;
public InicioCartasItemFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Inicializamos nuestra clase ViewModel
cartasViewModel = ViewModelProviders.of(getActivity()).get(CartasViewModel.class);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_inicio_cartas_item_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
adapter = new MyInicioCartasItemRecyclerViewAdapter(cartasList, getActivity());
recyclerView.setAdapter(adapter);
loadCartas();
}
return view;
}
//En este metodo debemos mandar a llamar al metodo de getListaCartas que se encuentra en nuestra clase ViewModel
//a este metodo le pasaremos un observador que nos permitira pasar los datos una vez cargados a nuestro adaptador
private void loadCartas() {
cartasViewModel.getListaCartas().observe(getActivity(), new Observer<List<ResponseCartas>>() {
@Override
public void onChanged(List<ResponseCartas> responseCartas) {
//a la lista que creamos en esta clase debemos igualarla a la lista que noes devuelve como respuesta nuestro servidor
cartasList = responseCartas;
//Una vez que rescuperamos la lista de cartas de servidor la enviamos a un metodo dentro de nuestra clase adapter
adapter.setData(cartasList);
}
});
}
}
The problem was in my onCreateView, I was instantiating the RecyclerView as the main view in my XML in a different way, I changed it to this and it worked: