Я создаю приложение чата с RecyclerView и классом List для хранения сообщений из базы данных Firebase и хочу прокрутить внизу recylcerView, когда активность чата открыта, но ошибка ..... Адаптер всегда возвращает ноль при вызове метод adapter.getItemcount (). Все сообщения отображаются без проблем.
Пользовательский адаптер Java-файл
public class ChatCustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private FirebaseUser mUser;
private String currentUser;
private List<ChatModel> msgList;
//Constructor
public ChatCustomAdapter(List<ChatModel> msgList) {
this.msgList = msgList;
FirebaseAuth mAuth = FirebaseAuth.getInstance();
mUser = mAuth.getCurrentUser();
currentUser = mUser.getUid();
}
@Override
public int getItemCount() {
return msgList.size();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if(viewType==0){
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_single__user_item_layout,parent,false);
return new SelfViewHolder(v);
}else if(viewType==1){
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_single_other_user_item_layout,parent,false);
return new OtherViewHolder(v);
}else{
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int layout = holder.getItemViewType();
ChatModel model = msgList.get(position);
String msg = model.getMsg();
if(layout==0){
SelfViewHolder sHolder = (SelfViewHolder)holder;
sHolder.setSelfMsgItems(msg);
}else if(layout==1){
OtherViewHolder oHolder = (OtherViewHolder)holder;
oHolder.setOtherMsgItems(msg);
}
}
@Override
public int getItemViewType(int position) {
super.getItemViewType(position);
int layout;
ChatModel model = msgList.get(position);
String from = model.getFrom();
if(from.equals(currentUser)){
layout=0;
}else{
layout=1;
}
return layout;
}
class SelfViewHolder extends RecyclerView.ViewHolder{
TextView msgtxt,timetxt;
SelfViewHolder(View itemView) {
super(itemView);
msgtxt = itemView.findViewById(R.id.chat_sinhle_user_item_textview);
timetxt = itemView.findViewById(R.id.chat_sinhle_user_item_timeview);
}
void setSelfMsgItems(String data){
msgtxt.setText(data);
}
}
class OtherViewHolder extends RecyclerView.ViewHolder{
TextView msgtxt,timetxt;
public OtherViewHolder(View itemView) {
super(itemView);
msgtxt = itemView.findViewById(R.id.chat_other_user_item_textview);
timetxt = itemView.findViewById(R.id.chat_other_user_item_timeview);
}
public void setOtherMsgItems(String data){
msgtxt.setText(data);
}
}
}
Активность чата java файл
protected void onStart() {
super.onStart();
//region RETREIVING MESSAGES FROM SERVER
msgQ = mDatabase.child("messages").child(current_user).child(otherusername).orderByChild("time");
msgQ.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ChatModel model = dataSnapshot.getValue(ChatModel.class);
mLsit.add(msgposition++, model);
mAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//endregion
recyclerView.scrollToPosition(mAdapter.getItemCount()-1);
}