android | java中使用recyclerview进行分页

7lrncoxx  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(333)

我想分页。我在firebase实时数据库中有帖子,我正在recyclerview上显示它们。当我进入feed活动时,我正在加载所有帖子。我不想这样,我想显示后10个职位,如果用户想看到更多,将向下滚动,更多的职位将下载。有很多关于它的例子,但我没有采用它来我的项目,我想要像reddit或instagram的东西。我该怎么做?
这是postadapter:

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder>{

public Context mContext;
public List<Post> mPost;

private FirebaseUser firebaseUser;

public PostAdapter(Context mContext, List<Post> mPost) {
    this.mContext = mContext;
    this.mPost = mPost;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.post_item,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    firebaseUser= FirebaseAuth.getInstance().getCurrentUser();
    Post post=mPost.get(position);

    if (post.getPostimage().equals("noimage")){
        holder.post_image.setVisibility(View.GONE);

        holder.post_text.setVisibility(View.VISIBLE);
        holder.post_text.setText(post.getPosttext());

        holder.post_title.setVisibility(View.VISIBLE);
        holder.post_title.setText(post.getPosttitle());

    }else {
        holder.post_text.setVisibility(View.GONE);

            holder.post_image.setVisibility(View.VISIBLE);
            Picasso.get().load(post.getPostimage()).into(holder.post_image);

        if (post.getPosttitle().equals("")){
            holder.post_title.setVisibility(View.GONE);
        }else {
            holder.post_title.setVisibility(View.VISIBLE);
            holder.post_title.setText(post.getPosttitle());
        }
    }
    publisherInfo(holder.username,post.getPublisher());

    holder.star.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (holder.star.getTag().equals("star")){
                FirebaseDatabase.getInstance().getReference().child("Stars").child(post.getPostid())
                        .child(firebaseUser.getUid()).setValue(true);
            }else {
                FirebaseDatabase.getInstance().getReference().child("Stars").child(post.getPostid())
                        .child(firebaseUser.getUid()).removeValue();
            }
        }
    });
    holder.comment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, CommentsActivity.class);
            intent.putExtra("postid",post.getPostid());
            intent.putExtra("publisherid",post.getPublisher());
            mContext.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {
    return mPost.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
    public ImageView post_image,star,comment,nsfw_image;
    public TextView username,star_count,post_title,comment_count,post_text;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        post_image=itemView.findViewById(R.id.post_image);
        post_text=itemView.findViewById(R.id.post_text);

        comment=itemView.findViewById(R.id.image_comment);
        username=itemView.findViewById(R.id.username);

        post_title=itemView.findViewById(R.id.post_title);
        comment_count=itemView.findViewById(R.id.comment_count);
    }
}

private void publisherInfo(TextView username,String userid){
    DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Users").child(userid);
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            User user=snapshot.getValue(User.class);
            username.setText(user.getUsername());
        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

}

这是主片段(提要活动)

public class HomeFragment extends Fragment {

private RecyclerView recyclerView;
private PostAdapter postAdapter;
private List<Post>postList;

private List<String>followingList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   View view = inflater.inflate(R.layout.fragment_home,container,false);

   recyclerView=view.findViewById(R.id.recycler_view);
   recyclerView.setHasFixedSize(true);
   LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
   linearLayoutManager.setReverseLayout(true);
   linearLayoutManager.setStackFromEnd(true);
   recyclerView.setLayoutManager(linearLayoutManager);
   postList=new ArrayList<>();
   postAdapter=new PostAdapter(getContext(),postList);
   recyclerView.setAdapter(postAdapter);

   checkFollowing();
   return view;
}

private void checkFollowing(){
    followingList=new ArrayList<>();
    DatabaseReference reference=FirebaseDatabase.getInstance().getReference("Follow")
            .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .child("following");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            followingList.clear();
            for (DataSnapshot dataSnapshot: snapshot.getChildren()){
                followingList.add(dataSnapshot.getKey());
            }
            readPosts();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

private void readPosts(){
    DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Posts");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            postList.clear();
            for (DataSnapshot dataSnapshot:snapshot.getChildren()){
                Post post=dataSnapshot.getValue(Post.class);
                for (String id:followingList){
                    if (post.getPublisher().equals(id)){
                        postList.add(post);
                    }
                }
            }
            postAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}
}

我需要你的帮助,谢谢你。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题