I have a Fragment with SwipeRefreshLayout and then RecyclerView as a child. When the Fragment is committed, it starts talking to the server to get some data and populate the CustomAdapter that will serve the RecyclerView.
The user can update the content by expanding it or by clicking a button in the ActionBar. In the latter case, I manually call swipeRefreshLayout.setRefreshing(true) . So there is still no problem.
The problem occurs when I manually call the update status during the first load of RecyclerView ( onStart() ). The progress indicator is not displayed, and I assume that RecyclerView is still empty, as it takes a few seconds to get all the data ...
I leave you the code.
Fragment Code:
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());
XML code:
<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>
What would be the best practice to solve this problem? Adding a second dummy SwipeRefreshLayout? Run a blank adapter immediately?
android android-recyclerview appcompat swiperefreshlayout
Emanuele
source share