SwipeRefreshLayout 带有一个空的 RecyclerView 和一个 TextView

SwipeRefreshLayout with a RecyclerView empty and a TextView

我遇到的问题在这里得到了完美的解释 http://innodroid.com/blog/post/recycler-empty-swipe-refresh 并且涉及处理 SwipeRefreshLayout 中的空 recyclerview。解决方案是 link 但我不知道如何实施它。

这是我的代码

<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>

这是我的 Fragment,我无法扩展另一个 class 因为我已经扩展了 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> {...}

我应该如何实施 link 的解决方案以在我的片段中使用它?我很困惑。

编辑

感谢第一个答案,它起作用了,但刷新圆圈动画还没有正确显示。

在布局中使用 yourpackagename.SwipeRefreshLayoutWithEmpty 代替 android.support.v4.widget.SwipeRefreshLayout。 (将 yourpackagename 替换为 SwipeRefreshLayoutWithEmpty class 的路径)。

除了 sKy 答案,你还应该在你的 Fragment 中替换:

private SwipeRefreshLayout refreshLayout;private SwipeRefreshLayoutWithEmpty refreshLayout;

refreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        new HackingBackgroundTask().execute();
                    }
                }
        );

refreshLayout.setOnRefreshListener(
                new SwipeRefreshLayoutWithEmpty.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        new HackingBackgroundTask().execute();
                    }
                }
        );