Android加载更多Firebase数据 问题是当我刷新片段以加载某些先前的消息时。 消息已下载但未显示,除非更改了片段并返回到聊天片段。
相关的OncreateView代码
customrecyclerviewAdapter = new MyAdapter(chalist,this,getchatdata);
// mrefreshlayout.setEnabled(false);
chalist.setAdapter(customrecyclerviewAdapter);
refreshing();
相关功能
刷新
protected void refreshing(){
usechildlistner();
mrefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mCurrentpage++;
itempos=0;
loadMoreData();
}
});
}
LoadMore
private void loadMoreData(){
chatdatabase.orderByKey().endAt(mLastKey).limitToLast(7).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
chatdatabaseinserver cdbis = dataSnapshot.getValue(chatdatabaseinserver.class);
String mLastKeyalt = dataSnapshot.getKey();
if (mPreviousKey.equals(mLastKeyalt)){
getchatdata.add(itempos++,cdbis);
}else{
mPreviousKey =mLastKey;
}
if(itempos == 1){
mLastKey = mLastKeyalt;
}
Log.d("TotalKeys","LastKey: "+mLastKey+" previous key:"+mPreviousKey+" messagekey"+ mLastKeyalt);
synchronized (customrecyclerviewAdapter) {
customrecyclerviewAdapter.notifyDataSetChanged();
}mrefreshlayout.setRefreshing(false);
chatlayout.scrollToPositionWithOffset(7,0);
}
});
onStart();
// mCurrentpage++;
// usechildlistner();
}
最后数据(打开碎片时首先加载数据)
public void usechildlistner(){
chatdatabase.limitToLast(mCurrentpage*total_items_to_Load
).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
chatdatabaseinserver cdbis =
dataSnapshot.getValue(chatdatabaseinserver.class);
int i = 0;
if(itempos == 1){
String mLastKeyalt = dataSnapshot.getKey();
// setItempos(itempos,mLastKeyalt,mLastKeyalt);
mLastKey = mLastKeyalt;
mPreviousKey = mLastKeyalt;
}
itempos++;
getchatdata.add(cdbis);
customrecyclerviewAdapter.notifyDataSetChanged();
chalist.scrollToPosition(getchatdata.size()-1);
}