我正在构建一个聊天应用程序,并且在聊天片段中,我目前只有一个聊天,其中只有一条消息。问题是单个项目连续调用onBindViewHolder
。我发现了一个相关的问题here,该问题说由itemview
的layoutparams引起的问题,但是我对itemview
的layoutparams没有做任何事情。由于连续呼叫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();
}
}
如果需要其他任何信息,请告诉我。任何帮助将不胜感激。
答案 0 :(得分:0)
在createView()中,您正在调用notifyDataSetChanged(),这将导致RecyclerView重新绑定并更新其所有内容。该过程的一部分包括对adapter.onBindViewHolder()的调用,以将数据集绑定到组成列表的各个项目视图。但是在adapter.onBindViewHolder()内部,您需要调用setupLastMessageData(),该内部再次调用notifyDataSetChanged(),从而再次开始循环。这将创建一个无限循环,其中包含notify-> bind-> notify-> bind
我的建议是让适配器外部有那些Firebase侦听器,侦听数据结构的必要更改和更新,并通过一次调用notifyDataSetChanged()将这些更改传递给适配器。您甚至可以通过不调用notifyDataSetChanged()来提高效率,而如果能够确定实际添加或更改了哪些消息,则只需从适配器调用更精细的notifyXXX Methods()。