具有空视图的FirebaseRecyclerAdapter [英] FirebaseRecyclerAdapter with empty view

查看:195
本文介绍了具有空视图的FirebaseRecyclerAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有许多方法可以为RecyclerView创建一个空视图。但我的问题是FirebaseRecyclerView。

我的布局是:

pre $ < RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =match_parent
android:layout_height =match_parent>

< android.support.v7.widget.RecyclerView
android:id =@ + id / feed_recycler_view
android:layout_width =match_parent
android :layout_height =match_parent
android:scrollbars =vertical/>

< ProgressBar
android:id =@ + id / feed_loading
style =?android:attr / progressBarStyle
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_centerInParent =true
android:visibility =gone/>
< / RelativeLayout>

所以我在RecyclerView填充它的项目之前显示加载ProgressBar。现在,如果服务器没有任何项目。在这种情况下,我的RecyclerView总是空的,我的加载ProgressBar总是可见的用户。所以,而不是无限期显示ProgressBar,我想显示一些空的布局。例如。 找不到数据或类似的消息。

最后的结果应该是:ProgressBar应该显示,直到数据被加载到RecyclerView和一旦数据被加载ProgressBar应该是不可见的。但是,如果服务器中没有数据,应该显示一些空的布局,而不是ProgressBar。

在普通的RecyclerView中,我们有一个数据集(一些ArrayList等),如果它是空的,那么我们可以显示空的布局。但在FirebaseRecyclerAdapter的情况下,我没有在我的活动或上下文中的快照的参考。我也没有任何回调,告诉我没有数据存在于服务器。

任何解决方法将帮助很多。

解决方案

这是我会尝试的。首先检查下面链接的问题的接受答案。它提供了一些关于Firebase查询如何工作的非常好的见解。我认为这个信息是可信的,因为答案是由Firebase团队中的某个人提供的:


$ b $因此,根据上面链接问题的答案,以及 FirebaseRecyclerAdapter FirebaseArray 使用 ChildEventListener 填充,我将在用于填充 FirebaseRecyclerAdapter 。就像这样:

$ $ p $ //创建数据库引用,用于
// FirebaseRecyclerAdapter和单个值事件侦听器
dbRef = FirebaseDatabase.getInstance()。getReference();
$ b $ //设置FirebaseRecyclerAdapter
mAdapter = new FirebaseRecyclerAdapter< Model,YourViewHolder>(
Model.class,R.layout.your_layout,YourViewHolder.class,dbRef){

@Override
public void populateViewHolder(YourViewHolder持有者,模型模型,int位置){

//您填充每个回收器视图项目的代码

};

mRecyclerView.setAdapter(mAdapter);

//为单个值事件添加监听器,这个函数的作用是
//对于FirebaseRecyclerAdapter的初始数据加载完成监听器
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot){
// onDataChange调用的方式如此删除进度条

//调用dataSnapshot.hasChildren()并基于
//返回值显示/隐藏空视图

//使用助手方法将观察者添加到RecyclerView
}

@Override
public void onCancelled(DatabaseError databaseError){
$ b}
});

这将处理RecyclerView的初始设置。在单值事件侦听器上调用 onDataChange 时,使用助手方法将观察者添加到FirebaseRecyclerAdapter中,以处理任何后续对数据库位置的添加/删除操作。 b
$ b

  mObserver = new RecyclerView.AdapterDataObserver(){
@Override
public void onItemRangeInserted(int positionStart,int itemCount){
//执行检查并显示/隐藏空视图

$ b @Override
public void onItemRangeRemoved(int positionStart,int itemCount){
//执行检查并显示/隐藏空视图
}
};
mAdapter.registerAdapterDataObserver(mObserver);


I know there are lot of ways to have an empty view for a RecyclerView. But my question is for FirebaseRecyclerView.

My layout is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

<ProgressBar
    android:id="@+id/feed_loading"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone"/>
</RelativeLayout>

So I am showing Loading ProgressBar before the RecyclerView fills its items. Now what if server doesn't have any item. In this situation my RecyclerView is empty always and my Loading ProgressBar always visible to user.

So instead of showing the ProgressBar for indefinite period, I want to show some empty layout. E.g. "No Data Found" or something similar message.

The final result should be: ProgressBar should be shown until data is loaded to RecyclerView and once data is loaded ProgressBar should be invisible. But if no data is present in server, some empty layout should be shown instead of ProgressBar.

In normal RecyclerView, we have a dataset (some ArrayList, etc) and if it is empty then we can show that empty layout. But in case of FirebaseRecyclerAdapter, I dont have the reference of Snapshot in my Activity or Context. Nor I have any callback which tells me that no data is present in server.

Any workaround will help a lot.

解决方案

Here is what I would try. First check out the accepted answer to the question linked below. It provides some very good insight into how Firebase queries work. I'd consider the info trusted since the answer is by someone on the Firebase team:

How to separate initial data load from incremental children with Firebase?

So, based on the answer to the question linked above and the fact that the FirebaseRecyclerAdapter is backed by a FirebaseArray which is populated using a ChildEventListener I would add a Single value event listener on the same database reference used to populate your FirebaseRecyclerAdapter. Something like this:

//create database reference that will be used for both the
//FirebaseRecyclerAdapter and the single value event listener
dbRef = FirebaseDatabase.getInstance().getReference();

//setup FirebaseRecyclerAdapter
mAdapter = new FirebaseRecyclerAdapter<Model, YourViewHolder>(
                Model.class, R.layout.your_layout, YourViewHolder.class, dbRef) {

                @Override
                public void populateViewHolder(YourViewHolder holder, Model model, int position){

                     //your code for populating each recycler view item

                };

mRecyclerView.setAdapter(mAdapter);

//add the listener for the single value event that will function
//like a completion listener for initial data load of the FirebaseRecyclerAdapter
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
             @Override
             public void onDataChange(DataSnapshot dataSnapshot) {
                 //onDataChange called so remove progress bar

                 //make a call to dataSnapshot.hasChildren() and based 
                 //on returned value show/hide empty view 

                 //use helper method to add an Observer to RecyclerView    
             }

             @Override
             public void onCancelled(DatabaseError databaseError) {

             }
      });

That would handle the initial setup of the RecyclerView. When onDataChange is called on the single value event listener use a helper method to add an observer to the FirebaseRecyclerAdapter to handle any subsequent additions/deletions to database location.

mObserver = new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                //perform check and show/hide empty view
            }

            @Override
            public void onItemRangeRemoved(int positionStart, int itemCount) {
                //perform check and show/hide empty view
            }
           };
mAdapter.registerAdapterDataObserver(mObserver);

这篇关于具有空视图的FirebaseRecyclerAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆