Firebase无限滚动列表视图在滚动上加载10个项目 [英] Firebase infinite scroll list view Load 10 items on Scrolling

查看:127
本文介绍了Firebase无限滚动列表视图在滚动上加载10个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来实现在我的Firebase应用程序中滚动的简单列表视图,但是我没有得到任何解决方法。我已经在互联网上尝试了2-3个教程和文档,但没有得到所需的结果。

在我的应用程序中,我想滚动像开始前10个列表项加载,然后每滚动下一个10或20个项目将加载,直到最后一个项目出现在底部





所以我试着用以下方法检索前10项:

 的ArrayList<事件> event = new ArrayList<>(); 

Dbref = FirebaseDatabase.getInstance()。getReference(/ event);
Dbref.startAt(1).limitToLast(10).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot snapshot: (Events.java,range query started!:+ dataSnapshot.getChildrenCount());
String event_id = snapshot.child(details ).child(event_id)。getValue()。toString();
e = new Event(event_id); //事件是列表项的模型类
event.add(e);
Log.e(ShowEventInfo:,+ event_id);
}
}
@Override
public void onCancelled(DatabaseError databaseError){

}
});
adapter = new Event_CustomAdapter(getActivity(),event);
ls.setAdapter(adapter); //这里的ls是ListView的实例

这个Youtube Video实现了这个功能:
https://www.youtube。 com / watch?annotation_id = annotation_3916642001& feature = iv& src_vid = YMJSBHAZsso& v = XwIKb_f0Y_w

在上面的代码中,生成日志来检查是否从firebase中获取数据,但我没有输出Android监视器。

我不知道如何在列表视图中实现Firebase滚动功能。我认为这对于那些在回收视图/列表视图中实现无限滚动的人来说是一个普遍的问题。

请帮我实施这个功能。谢谢。

解决方案

这里我们将使用 DatabaseReference limitToFirst(int) OR limitToLast(int)获取第一个或最后一个记录> FireBase



在下面的示例中,我使用 limitToFirst(int)只能从Firebase获取最高指定记录。在这里,我正在玩 oldestPostId ,这是我们记录的最后一个或第十五个关键字。

  private String oldestPostId; 
DatabaseReference Dbref = FirebaseDatabase.getInstance()。getReference(/ event);

$ b // ///从此处的FIREBASE获取第一个10条记录

Dbref.limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot child:dataSnapshot.getChildren()){

oldestPostId = child.getKey(); /// /这里我们保存最后的POST_ID从URS POST

dataSnapshot.getChildrenCount());
String event_id = snapshot.child(details)。child(event_id)。getValue()。toString();
e =新的事件(event_id); // Event是列表项的模型类
event.add(e);
Log.e(ShowEventInfo:,+ event_id);


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


});

从上面的代码中,我已经从firebase获得了前10条记录,现在我们将实现更多的功能所以我使用 Listview onscrolllistener
isScrollCompleted()我们将使用 orderByKey()获得更多的记录,其中 limitToFirst()方法。我已经实现了整个代码来获取 isScrollCompleted()的更多数据,请查看下面的例子。

  listView.setOnScrollListener(new OnScrollListener(){
private int currentVisibleItemCount;
private int currentScrollState;
private int currentFirstVisibleItem; $ b $ private int totalItem;
private LinearLayout lBelow;


@Override
public void onScrollStateChanged(AbsListView view,int scrollState){
// TODO自动生成方法存根
this.currentScrollState = scrollState;
this.isScrollCompleted();
}
$ b $ @Override
public void onScroll(AbsListView view,int firstVisibleItem,
int visibleItemCount,int totalItemCount){
// TODO自动生成的方法存根
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.totalItem = totalItemCount;


}
$ b $ private void isScrollCompleted(){
if(totalItem - currentFirstVisibleItem == currentVisibleItemCount
&& this.currentScrollState == SCROLL_STATE_IDLE){
/ **在这里做代码* /
Dbref.orderByKey()。startAt(oldestPostId).limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener(){
$覆盖$ b $ public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot child:dataSnapshot.getChildren()){

oldestPostId = child.getKey();
String event_id = snapshot.child(details)。child(event_id)。getValue()。toString();
e = new Event(event_id); //事件是列表项的模型类
event.add(e);
Log.e(ShowEventInfo:,+ event_id);
}
}

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

}
});

});


I'm looking for a way to implement a simple list view scrolling in my Firebase app, but I am not getting any way out how to implement this. I have already tried 2-3 tutorials and documentation available on the Internet, but didn't the required result.

In my app I want scrolling like at starting first 10 list items load then each time on scrolling next 10 or 20 items will load until the last item appears at the bottom of the list.

So I tried retrieving first 10 items the following way :

ArrayList<Event> event=new ArrayList<>();

Dbref = FirebaseDatabase.getInstance().getReference("/event");
Dbref.startAt(1).limitToLast(10).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Log.e("Events.java ", "range query started! : " + dataSnapshot.getChildrenCount());
                String event_id = snapshot.child("details").child("event_id").getValue().toString();
                e=new Event(event_id); //Event is a model class for list items
                event.add(e);
                Log.e("ShowEventInfo : ", "" + event_id);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    adapter=new Event_CustomAdapter(getActivity(),event); 
    ls.setAdapter(adapter); //here ls is instance of ListView

I referred this Youtube Video to implement this feature: (https://www.youtube.com/watch?annotation_id=annotation_3916642001&feature=iv&src_vid=YMJSBHAZsso&v=XwIKb_f0Y_w)

In the above code, as you can see I am generating Log to check if data is fetched from the firebase, but I got no output Android monitor.

I have no idea how can I implement Firebase Scrolling in my list view. I think this is a common problem for those who implement infinite-scroll in recycler view/ list view.

Could you please help me implementing this feature. Thanks.

解决方案

Here we will use the DatabaseReference;s methods which are limitToFirst(int) OR limitToLast(int) to get the first or last records from FireBase.

In below example i am using the limitToFirst(int) to get only top specified records from Firebase. Here i am playing with oldestPostId which is the last or 10th key of our record.

  private String oldestPostId;
  DatabaseReference Dbref = FirebaseDatabase.getInstance().getReference("/event");


  /////GETTING FIRST 10 RECORDS FROM THE FIREBASE HERE

        Dbref.limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot child : dataSnapshot.getChildren()) {

                    oldestPostId = child.getKey(); ////HERE WE ARE SAVING THE LAST POST_ID FROM URS POST

                    dataSnapshot.getChildrenCount());
                    String event_id = snapshot.child("details").child("event_id").getValue().toString();
                    e=new Event(event_id); //Event is a model class for list items
                    event.add(e);
                    Log.e("ShowEventInfo : ", "" + event_id);
                }      
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

From above code , i have get the first 10 records from firebase, Now we will implement load more functionality so i am using the Listview's onscrolllistener On isScrollCompleted() we will use orderByKey() for getting further 10 more record with limitToFirst() method. I have implemented the whole code to get further data on isScrollCompleted() , please check below example.

 listView.setOnScrollListener(new OnScrollListener() {
private int currentVisibleItemCount;
private int currentScrollState;
private int currentFirstVisibleItem;
private int totalItem;
private LinearLayout lBelow;


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub
    this.currentScrollState = scrollState;
    this.isScrollCompleted();               
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    // TODO Auto-generated method stub
    this.currentFirstVisibleItem = firstVisibleItem;
    this.currentVisibleItemCount = visibleItemCount;
    this.totalItem = totalItemCount;


}

private void isScrollCompleted() {
    if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
            && this.currentScrollState == SCROLL_STATE_IDLE) {
     /** To do code here*/
  Dbref.orderByKey().startAt(oldestPostId).limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child : dataSnapshot.getChildren()) {

            oldestPostId = child.getKey();
            String event_id = snapshot.child("details").child("event_id").getValue().toString();
                e=new Event(event_id); //Event is a model class for list items
                event.add(e);
                Log.e("ShowEventInfo : ", "" + event_id);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
     });

    }
    });

  });

这篇关于Firebase无限滚动列表视图在滚动上加载10个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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