具有空视图的 FirebaseRecyclerAdapter
FirebaseRecyclerAdapter with empty view
我知道有很多方法可以为 RecyclerView 创建一个空视图。但我的问题是关于 FirebaseRecyclerView 的。
我的布局是:
<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 始终为空,我的 Loading ProgressBar 始终对用户可见。
所以我不想无限期地显示 ProgressBar,而是想显示一些空的布局。例如。 "No Data Found" 或类似的消息。
最终结果应该是:ProgressBar 应该显示直到数据加载到 RecyclerView 并且一旦数据加载 ProgressBar 应该是不可见的。但是如果服务器中没有数据,应该显示一些空布局而不是 ProgressBar。
在正常的 RecyclerView 中,我们有一个数据集(一些 ArrayList 等),如果它是空的,那么我们可以显示该空布局。但是对于 FirebaseRecyclerAdapter,我的 Activity 或上下文中没有快照的引用。我也没有任何回调告诉我服务器中没有数据。
任何解决方法都会有很大帮助。
这是我会尝试的。首先查看下面链接的问题的已接受答案。它提供了一些关于 Firebase 查询如何工作的很好的见解。我认为这些信息是可信的,因为答案是由 Firebase 团队的某个人提供的:
因此,根据上面链接的问题的答案以及 FirebaseRecyclerAdapter
由使用 ChildEventListener
填充的 FirebaseArray
支持的事实,我会添加一个 Single用于填充 FirebaseRecyclerAdapter
的同一数据库引用上的值事件侦听器。像这样:
//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) {
}
});
这将处理 RecyclerView 的初始设置。当在单值事件侦听器上调用 onDataChange
时,使用辅助方法将观察者添加到 FirebaseRecyclerAdapter 以处理任何后续 additions/deletions 到数据库位置。
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);
答案来自:
如今的 FirebaseUI 适配器有一个 onDataChanged()
方法,您可以覆盖该方法以检测它们何时完成加载一组数据。
参见source code on github。从那里:
This method will be triggered each time updates from the database have been completely processed. So the first time this method is called, the initial data has been loaded - including the case when no data at all is available. Each next time the method is called, a complete update (potentially consisting of updates to multiple child items) has been completed.
You would typically override this method to hide a loading indicator (after the initial load) or to complete a batch update to a UI element.
FirebaseUI 示例应用覆盖 onDataChanged()
以隐藏其“正在加载”指示器:
public void onDataChanged() {
// If there are no chat messages, show a view that invites the user to add a message.
mEmptyListMessage.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
}
我知道有很多方法可以为 RecyclerView 创建一个空视图。但我的问题是关于 FirebaseRecyclerView 的。
我的布局是:
<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 始终为空,我的 Loading ProgressBar 始终对用户可见。
所以我不想无限期地显示 ProgressBar,而是想显示一些空的布局。例如。 "No Data Found" 或类似的消息。
最终结果应该是:ProgressBar 应该显示直到数据加载到 RecyclerView 并且一旦数据加载 ProgressBar 应该是不可见的。但是如果服务器中没有数据,应该显示一些空布局而不是 ProgressBar。
在正常的 RecyclerView 中,我们有一个数据集(一些 ArrayList 等),如果它是空的,那么我们可以显示该空布局。但是对于 FirebaseRecyclerAdapter,我的 Activity 或上下文中没有快照的引用。我也没有任何回调告诉我服务器中没有数据。
任何解决方法都会有很大帮助。
这是我会尝试的。首先查看下面链接的问题的已接受答案。它提供了一些关于 Firebase 查询如何工作的很好的见解。我认为这些信息是可信的,因为答案是由 Firebase 团队的某个人提供的:
因此,根据上面链接的问题的答案以及 FirebaseRecyclerAdapter
由使用 ChildEventListener
填充的 FirebaseArray
支持的事实,我会添加一个 Single用于填充 FirebaseRecyclerAdapter
的同一数据库引用上的值事件侦听器。像这样:
//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) {
}
});
这将处理 RecyclerView 的初始设置。当在单值事件侦听器上调用 onDataChange
时,使用辅助方法将观察者添加到 FirebaseRecyclerAdapter 以处理任何后续 additions/deletions 到数据库位置。
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);
答案来自:
如今的 FirebaseUI 适配器有一个 onDataChanged()
方法,您可以覆盖该方法以检测它们何时完成加载一组数据。
参见source code on github。从那里:
This method will be triggered each time updates from the database have been completely processed. So the first time this method is called, the initial data has been loaded - including the case when no data at all is available. Each next time the method is called, a complete update (potentially consisting of updates to multiple child items) has been completed.
You would typically override this method to hide a loading indicator (after the initial load) or to complete a batch update to a UI element.
FirebaseUI 示例应用覆盖 onDataChanged()
以隐藏其“正在加载”指示器:
public void onDataChanged() {
// If there are no chat messages, show a view that invites the user to add a message.
mEmptyListMessage.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
}