Я создаю приложение чата, и во фрагменте чатов В настоящее время у меня есть один чат с одним сообщением.Проблема в непрерывном звонке на onBindViewHolder
для одного предмета.Я нашел один связанный вопрос здесь , который говорит о проблеме, вызванной itemview
layoutparams Но я ничего не сделал с layoutparams itemview
.Из-за постоянного звонка на onBindViewHolder
прослушиватель кликов для открытия чата также не работает.проверьте Logcat
подробно:
2019-03-08 00:48:54.464 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.474 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.480 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.490 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.497 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.507 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.515 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.524 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.532 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.540 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.552 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.558 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.568 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.575 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.583 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.591 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.600 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.608 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.616 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.624 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.631 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.642 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.649 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
2019-03-08 00:48:54.658 1672-1672/com.invogen.messagingapp E/ChatAdapter: ChatPath = https://messagingapp-db3e5.firebaseio.com/Chats/-L_OWO_fra0oDMflLMTE/messages
2019-03-08 00:48:54.740 1672-1672/com.invogen.messagingapp E/ChatAdapter: Message Key = -L_OWO_jaN6ggbeoAlmI
Этот процесс не остановить, пока я не выйду из приложения.Вот моя firebase database
структура для этого узла чатов
Ниже приведен фрагмент кода моего чата, который вызывает ChatAdapter
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_chats, container, false);
this.mContext = view.getContext();
initViews(view);
mChatsDBReference = FirebaseDatabase.getInstance().getReference().child(AppConstants.CHATS_NODE);
fabCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Floating Action Button's Action code here for creating new chat group
}
});
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(new ChatAdapter(chatRoomChatsList, chatRoomKeysList));
mChatsDBReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
chatRoomChatsList.clear();
chatRoomKeysList.clear();
if (dataSnapshot.exists()) {
Log.e(TAG, "Snapshot Exits\nValue = " + dataSnapshot.toString());
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Chats chat = ds.getValue(Chats.class);
Log.e(TAG, "Chat Key = " + ds.getKey() + "\n DataSnapshot = " + ds.toString());
chatRoomChatsList.add(chat);
chatRoomKeysList.add(ds.getKey());
}
}
mRecyclerView.getAdapter().notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}
Вот мой ChatAdapter
класс
public class ChatAdapter extends RecyclerView.Adapter<ChatViewHolder> {
private String TAG = "ChatAdapter";
private List<Chats> chatRoomChatsList;
private Context mContext;
// private String pushKey;
private List<String> chatRoomChatsKeyList;
private List<FriendlyMessage> messageList;
public ChatAdapter(List<Chats> chatList, List<String> chatRoomChatsKeyList) {
this.chatRoomChatsList = chatList;
// this.pushKey = pushKey;
this.chatRoomChatsKeyList = chatRoomChatsKeyList;
}
@NonNull
@Override
public ChatViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int itemViewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chats_layout,
viewGroup, false);
Log.e(TAG, "onCreateView LayoutId = " + itemViewType + " Inside MyLayout");
return new ChatViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ChatViewHolder holder, int position) {
mContext = holder.chatTitleTV.getContext();
int pos = holder.getAdapterPosition();
setupLastMessageData(holder);
Chats chat = chatRoomChatsList.get(pos);
String chatTitle = chat.getChatName();
holder.chatTitleTV.setText(chatTitle);
holder.avatarTV.setText(chatTitle);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String pushkey = chatRoomChatsKeyList.get(holder.getAdapterPosition());
Log.e(TAG, "pushKey = " + pushkey);
Bundle bundle = new Bundle();
bundle.putString("pushKey", pushkey);
Intent intent = new Intent(mContext, ChatMessagesActivity.class);
intent.putExtras(bundle);
mContext.startActivity(intent);
}
});
}
private void setupLastMessageData(final ChatViewHolder holder) {
DatabaseReference mSingleChatReference =
FirebaseDatabase.getInstance().getReference()
.child(AppConstants.CHATS_NODE)
.child(chatRoomChatsKeyList.get(holder.getAdapterPosition()))
.child("messages");
Log.e(TAG, "ChatPath = " + mSingleChatReference);
mSingleChatReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
messageList = new ArrayList<>();
if (dataSnapshot.exists()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
FriendlyMessage msg = ds.getValue(FriendlyMessage.class);
Log.e(TAG, "Message Key = " + ds.getKey());
messageList.add(msg);
}
}
holder.lastMessageTV.setText(messageList.get(messageList.size() - 1).getMsgText());
holder.timeTV.setText(messageList.get(messageList.size() - 1).getMsgDate());
notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public int getItemCount() {
return chatRoomChatsList.size();
}
}
Если понадобится какая-либо другая информация, дайте мне знать.Любая помощь будет оценена.