使用RecyclerView为空的SwipeRefreshLayout和TextView

时间:2016-08-25 16:06:53

标签: android fragment android-recyclerview swiperefreshlayout

我所遇到的问题在http://innodroid.com/blog/post/recycler-empty-swipe-refresh中得到了很好的解释,并且可以在SwipeRefreshLayout中处理emtpy recyclerview。解决方案就在那个链接中,但我不知道如何实现它。

这是我的代码

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

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> {...}

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

修改

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

除了简单的答案,您还应该在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();
                    }
                }
        );