【发布时间】:2017-01-02 03:56:43
【问题描述】:
我遇到的问题在这里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,我无法扩展另一个类,因为我已经扩展了 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> {...}
我应该如何实现链接的解决方案以在我的片段中使用它?我很困惑。
编辑
感谢第一个答案,它可以正常工作,但刷新圈动画尚未正确显示。
【问题讨论】:
-
尝试在布局文件中使用 SwipeRefreshLayoutWithEmpty 而不是 SwipeRefreshLayout。
-
黑客后台任务,好名字!为什么在 HackingBackgroundTask 调用为空时不设置 visible(Gone) 为 recycler view 和 setvisible(Visible) text view?
-
@mhkore 你能具体说说你想在上面的问题中实现什么吗?
-
@Amit 具体来说是:我调用我的数据库 -> 如果为空 -> 改为 recyclerview 我显示一个 TextView “无数据连接” -> 一旦我刷新并且我的数据库已满 -> 显示 recyclerview项目
标签: android fragment android-recyclerview swiperefreshlayout