Firebase android pagination

后端 未结 10 1503
天命终不由人
天命终不由人 2020-11-28 09:14

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

10条回答
  •  渐次进展
    2020-11-28 09:29

    You can achieve it by using Firebase database paging library as below code... In this code, I have shown Post as an data model item and PostViewHolder as ViewHolder

            //Initialize RecyclerView
            mRecyclerView = findViewById(R.id.recycler_view);
            mRecyclerView.setHasFixedSize(true);
    
            LinearLayoutManager mManager = new LinearLayoutManager(this);
            mRecyclerView.setLayoutManager(mManager);
    
            //Initialize Database
            mDatabase = FirebaseDatabase.getInstance().getReference().child("posts");
    
            //Initialize PagedList Configuration
            PagedList.Config config = new PagedList.Config.Builder()
                    .setEnablePlaceholders(false)
                    .setPrefetchDistance(5)
                    .setPageSize(10)
                    .build();
    
            //Initialize FirebasePagingOptions
            DatabasePagingOptions options = new DatabasePagingOptions.Builder()
                    .setLifecycleOwner(this)
                    .setQuery(mDatabase, config, Post.class)
                    .build();
    
            //Initialize Adapter
            mAdapter = new FirebaseRecyclerPagingAdapter(options) {
                @NonNull
                @Override
                public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    return new PostViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false));
                }
    
                @Override
                protected void onBindViewHolder(@NonNull PostViewHolder holder,
                                                int position,
                                                @NonNull Post model) {
                    holder.setItem(model);
                }
    
                @Override
                protected void onLoadingStateChanged(@NonNull LoadingState state) {
                    switch (state) {
                        case LOADING_INITIAL:
                        case LOADING_MORE:
                            // Do your loading animation
                            mSwipeRefreshLayout.setRefreshing(true);
                            break;
    
                        case LOADED:
                            // Stop Animation
                            mSwipeRefreshLayout.setRefreshing(false);
                            break;
    
                        case FINISHED:
                            //Reached end of Data set
                            mSwipeRefreshLayout.setRefreshing(false);
                            break;
    
                        case ERROR:
                            retry();
                            break;
                    }
                }
    
                @Override
                protected void onError(@NonNull DatabaseError databaseError) {
                    super.onError(databaseError);
                    mSwipeRefreshLayout.setRefreshing(false);
                    databaseError.toException().printStackTrace();
                }
            };
    
            //Set Adapter to RecyclerView
            mRecyclerView.setAdapter(mAdapter);
    

    Just visit this below URL for reference https://firebaseopensource.com/projects/patilshreyas/firebaserecyclerpagination/app/readme.md/ Here you will find implementation of library which helps to implement Pagination of firebase data in RecyclerView

    I hope this will help you!

提交回复
热议问题