我想用FirebaseRecyclerAdapter Class实现分页。
我在谷歌上找到了很多来源来实现它。 但失败了。
当我向下滚动屏幕时,如果我到达页面项目的末尾 我想每页显示三个项目。
public class LoggedActivity extends AppCompatActivity {
private Button logOutButton, postPageButton;
private Session session;
private RecyclerView mPostList;
private DatabaseReference mDatabase;
private static Activity thisActivity = null;
private Parcelable mLayoutManagerSavedState;
public static int orderNum = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logged);
thisActivity = this;
logOutButton = findViewById(R.id.logoutButton);
postPageButton = findViewById(R.id.postButton);
mDatabase = FirebaseDatabase.getInstance().getReference().child("post");
mPostList = findViewById(R.id.post_list);
mPostList.setHasFixedSize(true);
mPostList.setLayoutManager(new LinearLayoutManager(this));
Intent intent = getIntent();
final String username = intent.getStringExtra("username");
session = new Session(this);
if(!session.loggedIn()){
logout();
}
logOutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logout();
}
});
postPageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoggedActivity.this, PostActivity.class);
startActivity(intent);
intent.putExtra("username", username);
}
});
}
下面的代码,我的Firebase Recycler Adapter有“populate viewholder”方法
@Override
protected void onStart() {
super.onStart();
final Query DBquery = FirebaseDatabase.getInstance().getReference().child("post").orderByChild("count");
FirebaseRecyclerAdapter<Post, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(
Post.class,
R.layout.post_row,
PostViewHolder.class,
DBquery
// mDatabase
) {
@Override
protected void populateViewHolder(PostViewHolder viewHolder, Post model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(), model.getImage());
viewHolder.setDate(model.getDate());
viewHolder.init(position, model);
}
};
mPostList.setAdapter(firebaseRecyclerAdapter);
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
View mView;
public PostViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc){
TextView post_desc = mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setImage(Context ctx, String image){
ImageView post_image = mView.findViewById(R.id.post_image);
Glide.with(ctx)
.load(image)
.into(post_image);
}
public void setDate(String date){
TextView post_date = mView.findViewById(R.id.post_date);
post_date.setText(date);
}
public void init(final int position, final Post model){
mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(thisActivity, "게시물로 이동", Toast.LENGTH_LONG).show();
Intent intent = new Intent(thisActivity, MyPostActivity.class);
intent.putExtra("image", model.getImage());
intent.putExtra("title", model.getTitle());
intent.putExtra("text", model.getDesc());
thisActivity.startActivity(intent);
}
});
}
}
private void logout(){
FirebaseAuth.getInstance().signOut();
session.setLoggedIn(false);
startActivity(new Intent(LoggedActivity.this, MainActivity.class));
finish();
}
}