Ho un Fragment
con un SwipeRefreshLayout
e quindi RecyclerView
come figlio. Quando il frammento viene eseguito, inizia a parlare con un server per recuperare alcuni dati e popolare un Adattatore personalizzato che servirà a RecyclerView.SwipeRefreshLayout è nascosto su RecyclerView vuoto
L'utente può aggiornare il contenuto scorrendo verso il basso o premendo un pulsante su ActionBar. In quest'ultimo caso, chiamerò manualmente swipeRefreshLayout.setRefreshing(true)
. Quindi nessun problema fino ad ora.
Il problema sorge quando richiama manualmente lo stato di aggiornamento durante il primo caricamento di RecyclerView (onStart()
). L'indicatore di avanzamento non viene visualizzato e presumo perché RecyclerView è ancora vuoto poiché richiede alcuni secondi per recuperare tutti i dati ...
Vi lascio un po 'di codice.
frammento di codice: il codice
public class MyFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.fragment_stop, container, false);
mRecyclerView = (RecyclerView) mView.findViewById(R.id.recyclerView1);
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
DividerItemDecoration.VERTICAL_LIST));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//SwipeRefreshLayout retrieved and configured
swipeLayout = (SwipeRefreshLayout) mView.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorSchemeResources(
R.color.primaryColor,
R.color.cyan_500,
R.color.light_green_500,
R.color.amber_500);
return mView;
}
...
@Override
public void onStart() {
super.onStart();
executeSchedule(); //First execution and data loading.
}
...
@Override
public void onRefresh() {
executeSchedule();
}
...
public void executeSchedule() {
new Schedule().execute();
}
private class Schedule extends AsyncTask<Void, Void, Void> {
...
@Override
protected void onPreExecute() {
super.onPreExecute();
if(!swipeLayout.isRefreshing()) {
swipeLayout.setRefreshing(true); //set refresh state
}
}
@Override
protected Void doInBackground(Void... params) {
...
}
@Override
protected void onPostExecute(Void result) {
adap = new CustomAdapter(getActivity(), ...);
mRecyclerView.setAdapter(adap);
swipeLayout.setRefreshing(false);
}
}
XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="2dp"
android:layout_marginTop="5dp"
android:divider="#e0e0e0"
tools:context=".MyFragment" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
</FrameLayout>
Quale sarebbe la prassi migliore per affrontare questa situazione? Aggiunta di un secondo dummy SwipeRefreshLayout? Forzare immediatamente un adattatore vuoto?
Grazie. Non lo sapevo. Sicuramente è una soluzione molto migliore rispetto a chiamare 'setProgressViewOffset()' – Emanuele