在Firebase Recycler适配器中仅加载x个项目 [英] Load only x items in Firebase Recycler Adapter
问题描述
我有一个Firebase FirebaseRecyclerAdapter
,在数据库的Firebase项目中,我有30多个值,在我的应用中,我只想显示前x个项目.
Hi so I have one Firebase FirebaseRecyclerAdapter
and in my Firebase project in my database I have over 30 values and in my app I want to display only the first x items.
我在互联网上进行搜索,但是找不到如何只能加载x项的内容.
I searched on internet but I didn't find how I could load only x items.
这是我的代码:
mAdapter = new FirebaseRecyclerAdapter<Post, Holder>(
Post.class,
R.layout.item_layout,
Holder.class,
mRef
){
@Override
public void onBindViewHolder(Holder viewHolder, int position) {
Post post = (Post) this.getItem(position);
((Holder) viewHolder).bind(post);
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view;
view = inflater.inflate(R.layout.item_layout , parent, false);
return new Holder(view);
}
}
这是我的持班人:
public static class EventsOldHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public EventsOldHolder(View itemView) {
}
public void bind(final Post post) {
}
@Override
public void onClick(View view) {
}
我知道,当position的值大于x时,我可以隐藏元素,但是如果我有很多项目并隐藏所有元素,则我的应用程序运行速度会变慢,并且如果可以帮助我提供更好的解决方案. >
I know I can hide the elements when position has a higher value than x but if I have a lot of items and hide all of them my app will work slower and if you can help me with a better solution.
推荐答案
要解决此问题,您应该将query
而不是mRef
传递给FirebaseRecyclerAdapter
构造函数,我认为它是DatabaseReference
对象
To solve this, you should pass to the FirebaseRecyclerAdapter
constructor a query
instead of the mRef
, which I assume is a DatabaseReference
object.
假设您具有一个如下所示的数据库结构:
Assuming that you have a database structure that looks like this:
Firebase-root
|
--- posts
|
--- postId
|
--- title: "Post Title"
|
--- //Other details
要仅显示前5个项目,查询应如下所示:
To display only the first 5 items, the query should look like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("posts").orderByChild("title").limitToFirst(5);
query.addListenerForSingleValueEvent(/* ... */);
这篇关于在Firebase Recycler适配器中仅加载x个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!