如何在Android Studio中使用Firebase使帖子部分加载并缓存帖子?

时间:2019-06-26 11:45:35

标签: android firebase firebase-realtime-database firebaseui

我使用FirebaseRecyciclerView创建了一个新闻源,但问题是它们一次加载了所有内容,并且滚动非常缓慢且滞后。我想添加部分加载和缓存。像Facebook,Twitter,Instagram等。我曾经尝试过Volley,但我无法弄清楚。有更多知识的人可以告诉我什么是解决我的问题的最佳方法。

我尝试了这个答案,但没有结果 Firebase load data dynamically on scrolling

private void DisplayAllUsersPosts()
{
    Query SortPostsInDescendingOrder = PostsRef.orderByChild("counter");

    FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<Posts, PostsViewHolder>
                    (
                            Posts.class,
                            R.layout.all_posts_layout,
                            PostsViewHolder.class,
                            SortPostsInDescendingOrder
                    )
            {
                @Override
                protected void populateViewHolder(PostsViewHolder viewHolder, Posts model, int position)
                {
                    final String PostKey = getRef(position).getKey();

                    viewHolder.setFullname(model.getFullname());
                    viewHolder.setTime(model.getTime());
                    viewHolder.setDate(model.getDate());
                    viewHolder.setDescription(model.getDescription());
                    viewHolder.setUser_thumb_image(getApplicationContext(), model.getUser_thumb_image());
                    viewHolder.setPostimage(getApplicationContext(), model.getPostimage());

                    viewHolder.setLikeButtonStatus(PostKey);

                    viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent clickPostIntent = new Intent(MainActivity.this, ClickPostActivity.class);
                            clickPostIntent.putExtra("PostKey", PostKey);
                            startActivity(clickPostIntent);
                        }
                    });

                    viewHolder.CommentPostButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent commentsIntent = new Intent(MainActivity.this, CommentsActivity.class);
                            commentsIntent.putExtra("PostKey", PostKey);
                            startActivity(commentsIntent);
                        }
                    });

                    viewHolder.LikePostButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            LikeChecker = true;

                            LikesRef.addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    if(LikeChecker.equals(true)){
                                        if(dataSnapshot.child(PostKey).hasChild(currentUserID)){
                                            LikesRef.child(PostKey).child(currentUserID).removeValue();
                                            LikeChecker = false;
                                        }else{
                                            LikesRef.child(PostKey).child(currentUserID).setValue(true);
                                            LikeChecker = false;
                                        }
                                    }
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });
                        }
                    });
                }
            };
    postList.setAdapter(firebaseRecyclerAdapter);
}

0 个答案:

没有答案