mhkore mhkore - 3 months ago 13
Android Question

SwipeRefreshLayout with a RecyclerView empty and a TextView

The problem I have is perfectly explained here http://innodroid.com/blog/post/recycler-empty-swipe-refresh and is realted to dealing with emtpy recyclerview inside a SwipeRefreshLayout. The solution is in that link but I don't have idea how to implement it.

This is my code

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:id="@+id/rv"
xmlns:android="http://schemas.android.com/apk/res/android"/>

<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="No hay conexión con el servidor" />
</FrameLayout>

</android.support.v4.widget.SwipeRefreshLayout>


and this is my Fragment where I cannot extend another class because I already extend Fragment.

public class FragmentNoticias extends Fragment {

private RVAdapter adapter;
private String mNoticias = "Resumen";
private SwipeRefreshLayout refreshLayout;
private Cursor cursor;

public interface RefreshCall {

boolean onMethodRefreshCall();
}

private RefreshCall mRefreshCall;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cursor = FeedDatabase.getInstance(getActivity()).obtenerEntradas(mNoticias);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

View root = inflater.inflate(R.layout.recycler_view, container, false);
RecyclerView rv = (RecyclerView) root.findViewById(R.id.rv);

rv.setHasFixedSize(true);

//hace que el RecyclerView se muestre por defecto con un layout linear
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);

adapter = new RVAdapter(
getActivity(),
cursor);
rv.setAdapter(adapter);


// Obtener el refreshLayout
refreshLayout = (SwipeRefreshLayout) root.findViewById(R.id.swipeRefresh);

refreshLayout.setColorSchemeResources(
R.color.s1,
R.color.s2,
R.color.s3
);

// Iniciar la tarea asíncrona al revelar el indicador
refreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new HackingBackgroundTask().execute();
}
}
);

try {
this.mRefreshCall = ((RefreshCall) getActivity());
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement AdapterCallback.");
}


return root;
}

private class HackingBackgroundTask extends AsyncTask<Void, Void, Cursor> {...}


How should I implement the solution of the link to use it in my fragment ? I'm confused.

EDIT

Thanks to the first answer it works but the refresh circle animation isn't showed correctly yet.

sJy sJy
Answer

In place of android.support.v4.widget.SwipeRefreshLayout use yourpackagename.SwipeRefreshLayoutWithEmpty in your layout. (Replace yourpackagename with the path to your SwipeRefreshLayoutWithEmpty class).