Staggeredgridview inside swiperefreshlayout goes empty on swipe refresh

I have a StaggeredGridview inside a SwipeRefreshLayout which goes empty when i refreshes the grid using the swipe action even though my list is not empty (data). If i use the LinearLayoutManager instead of StaggeredGridLayoutManager everything is working fine (i get a listView instead of staggeredgrid). When i change the LinearLayoutManager to StaggeredGridLayoutManager again the view goes empty on refresh

This is how i set LinearLayoutManager and StaggeredGridLayoutManager to my recyclerView

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.addItemDecoration(new VerticalDividerItemDecoration(getActivity(), R.drawable.divider_discover));
        recyclerView.setLayoutManager(linearLayoutManager);

StaggeredGridLayoutManager stgaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL);
        recyclerView.setLayoutManager(stgaggeredGridLayoutManager);

Any help is appreciated. Thanks in advance.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical" />
        </android.support.v4.widget.SwipeRefreshLayout>

    </FrameLayout>

    <TextView
        android:id="@+id/tv_empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/list_empty"
        android:textSize="20sp"
        android:visibility="gone" />

</RelativeLayout>

Upvotes: 0

Views: 433

Answers (1)

Solanki Zeel
Solanki Zeel

Reputation: 528

I solved my issue using

re-attach layout manager before notifyDataSetChanged()

                    recy_latest.setHasFixedSize(true);
                    layoutManager = new StaggeredGridLayoutManager(InitApplication.getLayoutCount(),StaggeredGridLayoutManager.VERTICAL);
                    recy_latest.setLayoutManager(layoutManager);
                    recy_latest.setItemAnimator(new DefaultItemAnimator());
                    wallpaperAdapter.notifyDataSetChanged();

Upvotes: 0

Related Questions