这是我在我的应用中显示NewsFeed的活动。 我可以从Firebase实时数据库中读取NewsFeed的数据。
public class LoggedActivity extends AppCompatActivity {
private Button logOutButton, postPageButton;
private Session session;
private RecyclerView mPostList;
private DatabaseReference mDatabase;
private DocumentReference postRef;
private static Activity thisActivity = null;
private LinearLayoutManager mLayoutManager;
private boolean userScrolled = false;
private int pastVisiblesItems, visibleItemCount, totalItemCount;
private int itemsPerPage = 2;
private int currentPage = 0;
public static int orderNum = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logged);
thisActivity = this;
mDatabase = FirebaseDatabase.getInstance().getReference().child("post");
mPostList = findViewById(R.id.post_list);
mPostList.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mPostList.setLayoutManager(mLayoutManager);
}
@Override
protected void onStart() {
super.onStart();
final Query DBquery = FirebaseDatabase.getInstance().getReference().child("post").orderByChild("count");
final 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);
这是新闻Feed中的ScrollListener。 它工作正常。
mPostList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
userScrolled = true;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager
.findFirstVisibleItemPosition();
但问题出在这里。 在这个if语句中,我需要从实时数据库中获取新数据。 但我不知道该怎么做。
if (userScrolled && (visibleItemCount + pastVisiblesItems) == totalItemCount){
userScrolled = false;
// fetch new data here.
}
}
});
}
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);
}
}
}
}
此代码为Post.java Class
public class Post {
private String title, desc, image, date;
public Post(){
}
public Post(String title, String desc, String image, String date) {
this.title = title;
this.desc = desc;
this.image = image;
this.date = date;
}
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
public String getDate() {
return date;
}
public void setTitle(String title) {
this.title = title;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public void setDate(String date){
this.date = date;
}
}