I am using Paging 3 library with ViewMode,Room and LiveData for my chat app. My app is working fine till old text messages items loaded at first but issues start when i start sending new message (Adding new item to list). When i check positions of new items by OnClick method it always shows 0 positions for every new items added and start 1 2 3... from first old items. In my chat app every new item positions should be at 0 and other items move accordingly as all chat app work like. I am not sure where i am making mistake.
Chat Fragment For Adding Text Message.
List<ChatEntity_TableColums> message = new ArrayList<>();
ChatEntity_TableColums chatEntity_tableColums = new ChatEntity_TableColums(Chat_Msg,Chat_TimeStamp);
message.add(chatEntity_tableColums);
ChatRoomDatabase.getInstance(MyApplication.getmContext(), 1).chatDAO().InsertChat(message);
Room Database Insert Query + Pagination Chat Query
@Insert(onConflict = OnConflictStrategy.IGNORE)
void InsertChat(List<ChatEntity_TableColums> chat);
@Query("SELECT * FROM Chat WHERE OTHER_USER_ID = :UserID ORDER BY ID DESC")
PagingSource<Integer, ChatEntity_TableColums> getPaginationChat(String UserID);
ViewModel Class
public class Chat_ViewModel extends ViewModel {
private static final String TAG = "Chat View Model";
public final LiveData<PagingData<ChatEntity_TableColums>> chatList;
public Chat_ViewModel() {
Pager<Integer, ChatEntity_TableColums> pager = new Pager(
// Create new paging config
new PagingConfig(20, // Count of items in one page
20, // Number of items to prefetch
false, // Enable placeholders for data which is not yet loaded
20, // initialLoadSize - Count of items to be loaded initially
200),// maxSize - Count of total items to be shown in recyclerview
() -> ChatRoomDatabase.getInstance(MyApplication.getmContext(), 1).chatDAO().getPaginationChat(String.valueOf(Session.getOtherUserID()))
);
CoroutineScope coroutineScope = ViewModelKt.getViewModelScope(this);
chatList = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager),coroutineScope);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int pos) {
int position = holder.getAbsoluteAdapterPosition();
ChatEntity_TableColums wrapper = getItem(pos);
viewHolderText.textMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: Text Position = "+position);
}
});
break;
I am using Paging 3 library with ViewMode,Room and LiveData for my chat app. My app is working fine till old text messages items loaded at first but issues start when i start sending new message (Adding new item to list). When i check positions of new items by OnClick method it always shows 0 positions for every new items added and start 1 2 3... from first old items. In my chat app every new item positions should be at 0 and other items move accordingly as all chat app work like. I am not sure where i am making mistake.
Chat Fragment For Adding Text Message.
Main Screen Fragment (Chat Screen)
Chat Adapter (PagingDataAdapter)
DiffUtill.itemcallaback
If i miss anything then let me know so i can update my code. Can anyone figure out what i am doing wrong in my code which makes it
Note: I am not using adapter.notifyDataSetChanged(); anywhere, i depend on live data and viewmodel working.