顯示具有 聊天室 標籤的文章。 顯示所有文章
顯示具有 聊天室 標籤的文章。 顯示所有文章

2021年10月28日 星期四

Android Studio 聊天室下拉加載歷史訊息

 一開始需求是每次有新的訊息要在最下面

然後就很突然的,將 RecyclerView 設置為倒序

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), 
LinearLayoutManager.VERTICAL, true); // 列表翻轉
linearLayoutManager.setStackFromEnd(true); // 列表在底部開始展示, 反轉後由上面開始展示
rvMessagesChat.setLayoutManager(linearLayoutManager);
rvMessagesChat.setHasFixedSize(true);
rvMessagesChat.setAnimation(null);


但是在加載新資料的時候,舊資料會被擠下去

int beforeSize = mList.size();
int afterSize = model.vModelRecord.size();
profileMessagesChatAdapter.addList(model.vModelRecord);
profileMessagesChatAdapter.notifyItemRangeInserted(beforeSize, afterSize);


所以需要新增這行,這樣就不會有擠壓的動畫

rvMessagesChat.setAnimation(null);

Android Studio IDE 錯誤

 :app:compile xxxxx JavaWithJavac FAILED An exception has occurred in the compiler (1.8.0_312). Please file a bug against the Java compiler ...