本文介绍了Firebase的Android分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序将显示存储在Firebase上的视频。视频列表需要分页获取最近20个视频。





这里是我认为会工作的代码

  private void getVideos(){

Query videosQuery = FirebaseUtil.getVideosRef();
videosQuery.startAt(0);
videosQuery.endAt(1);

ChildEventListener videosChildEventListener = new ChildEventListener(){
@Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String s){
String date = dataSnapshot.getKey();
String temp = date;

$ b @Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot){

}
$ b @Override
public void onChildMoved(DataSnapshot dataSnapshot,String s){


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

Log.d(tag,database error);
}
};

$ b ValueEventListener videoValueEventListener = new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
String date = dataSnapshot.getKey() ;
String temp = date;

long count = dataSnapshot.getChildrenCount();
String value = dataSnapshot.getValue()。toString();
temp = value;

$ b @Override
public void onCancelled(DatabaseError databaseError){
Log.d(tag,database error);
}
};
// videosQuery.addChildEventListener(videosChildEventListener);
videosQuery.addValueEventListener(videoValueEventListener);






但是,上面的代码检索整个视频列表,而不是有限的视频。如何实现分页

解决方案

下面是我用于分页显示最新节点的代码。 ()>

  public void getImages(){
Query imagesQuery = FirebaseDatabase.getInstance()。getReference()。child(englishDps ).child(mChildName).orderByKey()limitToLast(21)。

ChildEventListener childEventListener = new ChildEventListener(){
@Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String s){
Image image = dataSnapshot.getValue(Image。类);
image.setNodeKey(dataSnapshot.getKey());

mTempImages.add(image);
if(mTempImages.size()== 21){
mLastKey = mTempImages.get(0).getNodeKey();
Collections.reverse(mTempImages);
mTempImages.remove(mTempImages.size() - 1);
mImages.addAll(mTempImages);
setAdapter();


$ b @Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot){

}
$ b $ @Override
public void onChildMoved(DataSnapshot dataSnapshot,String s){


@Override
public void onCancelled(DatabaseError databaseError){
if(isAdded()){
Toast.makeText getActivity(),加载更多图片的问题...,Toast.LENGTH_LONG).show();
}
}
};

imagesQuery.addChildEventListener(childEventListener);


$ b @Override
public void getMoreImages(){
if(!mGettingMoreImages){
mGettingMoreImages = true;
Query imagesQuery = FirebaseDatabase.getInstance()。getReference(englishDps)。child(mChildName).orderByKey()。endAt(mLastKey).limitToLast(21);

ChildEventListener childEventListener = new ChildEventListener(){
@Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String s){
Image image = dataSnapshot.getValue(Image。类);
image.setNodeKey(dataSnapshot.getKey());
mMoreImages.add(image);
if(mMoreImages.size()== 21){
mLastKey = mMoreImages.get(0).getNodeKey();
Collections.reverse(mMoreImages);
mMoreImages.remove(mMoreImages.size() - 1);
mImages.addAll(mMoreImages);
mMoreImages.clear();
mGettingMoreImages = false;
mImagesAdapter.notifyDataSetChanged();
return;
}

if(mLastKey.equalsIgnoreCase(image.getNodeKey())){
Collections.reverse(mMoreImages);
mImages.addAll(mMoreImages);
mMoreImages.clear();
mGettingMoreImages = false;
mImagesAdapter.onNoMoreImages();
;
mImagesAdapter.notifyDataSetChanged();


$ b @Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot){

}
$ b $ @Override
public void onChildMoved(DataSnapshot dataSnapshot,String s){


@Override
public void onCancelled(DatabaseError databaseError){
if(isAdded()){
Toast.makeText getActivity(),加载更多图片的问题...,Toast.LENGTH_LONG).show();
}
}
};

imagesQuery.addChildEventListener(childEventListener);
}
}


I'm building an app which will show videos stored on firebase. The list of videos needs to be paginated fetching most recent 20 videos at a time.

Here is the code I thought would work

  private void getVideos() {

        Query videosQuery = FirebaseUtil.getVideosRef();
        videosQuery.startAt(0);
        videosQuery.endAt(1);

        ChildEventListener videosChildEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String date = dataSnapshot.getKey();
                String temp = date;
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

                Log.d(tag, "database error");
            }
        };


        ValueEventListener videoValueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String date = dataSnapshot.getKey();
                String temp = date;

               long count =  dataSnapshot.getChildrenCount();
                String value = dataSnapshot.getValue().toString();
                temp = value;
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d(tag, "database error");
            }
        };
//        videosQuery.addChildEventListener(videosChildEventListener);
        videosQuery.addValueEventListener(videoValueEventListener);

    }

But above code retrieves entire list of videos instead of limited videos. How can pagination be implemented.

解决方案

Below is the code I'm using for pagination which shows the latest node first.

      public void getImages() {
            Query imagesQuery = FirebaseDatabase.getInstance().getReference().child("englishDps").child(mChildName).orderByKey().limitToLast(21);

            ChildEventListener childEventListener = new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    Image image = dataSnapshot.getValue(Image.class);
                    image.setNodeKey(dataSnapshot.getKey());

                    mTempImages.add(image);
                    if (mTempImages.size() == 21) {
                        mLastKey = mTempImages.get(0).getNodeKey();
                        Collections.reverse(mTempImages);
                        mTempImages.remove(mTempImages.size() - 1);
                        mImages.addAll(mTempImages);
                        setAdapter();
                    }
                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    if (isAdded()) {
                        Toast.makeText(getActivity(), "Problem loading more images...", Toast.LENGTH_LONG).show();
                    }
                }
            };

            imagesQuery.addChildEventListener(childEventListener);
        }


  @Override
    public void getMoreImages() {
        if (!mGettingMoreImages) {
            mGettingMoreImages = true;
            Query imagesQuery = FirebaseDatabase.getInstance().getReference("englishDps").child(mChildName).orderByKey().endAt(mLastKey).limitToLast(21);

            ChildEventListener childEventListener = new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    Image image = dataSnapshot.getValue(Image.class);
                    image.setNodeKey(dataSnapshot.getKey());
                    mMoreImages.add(image);
                    if (mMoreImages.size() == 21) {
                        mLastKey = mMoreImages.get(0).getNodeKey();
                        Collections.reverse(mMoreImages);
                        mMoreImages.remove(mMoreImages.size() - 1);
                        mImages.addAll(mMoreImages);
                        mMoreImages.clear();
                        mGettingMoreImages = false;
                        mImagesAdapter.notifyDataSetChanged();
                        return;
                    }

                    if (mLastKey.equalsIgnoreCase(image.getNodeKey())) {
                        Collections.reverse(mMoreImages);
                        mImages.addAll(mMoreImages);
                        mMoreImages.clear();
                        mGettingMoreImages = false;
                        mImagesAdapter.onNoMoreImages();
                        ;
                        mImagesAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    if (isAdded()) {
                        Toast.makeText(getActivity(), "Problem loading more images...", Toast.LENGTH_LONG).show();
                    }
                }
            };

            imagesQuery.addChildEventListener(childEventListener);
        }
    }

这篇关于Firebase的Android分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 00:49