我正在开发一个聊天应用程序,但在使用 RecyclerView 显示内容时遇到问题。我正在使用 ViewBinding。
这是我面临的问题:尽管我做出了努力,RecyclerView 仍不显示任何内容。我已经实现了 ViewBinding 来绑定视图,但我不确定是什么导致了这个问题。
这是我的代码。此外,我还按照这些视频来构建应用程序:Android 聊天应用程序开发。
在
ChatMessageAdapter.java
:
import ...
public class ChatMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<ChatMessage> chatMessageList;
public ChatMessageAdapter(List<ChatMessage> chatMessageList) {
this.chatMessageList = chatMessageList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// Log.d("tag", "why!!!!!!");
if (viewType == Constants.VIEW_TYPE_SENT) {
return new SentMassageViewHolder(
ItemContainerSentMessageBinding.inflate(
LayoutInflater.from(parent.getContext()),
parent,
false
)
);
} else {
return new ReceivedMassageViewHolder(
ItemContainerReceivedMessageBinding.inflate(
LayoutInflater.from(parent.getContext()),
parent,
false
)
);
}
// View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_container_sent_message, parent, false);
// return new testViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == Constants.VIEW_TYPE_SENT) {
((SentMassageViewHolder) holder).setData((chatMessageList.get(position)));
} else {
((ReceivedMassageViewHolder) holder).setData(chatMessageList.get(position));
}
// ((testViewHolder) holder).test.setText(chatMessageList.get(position).getMessage());
}
static class testViewHolder extends RecyclerView.ViewHolder {
TextView test;
public testViewHolder(@NonNull View itemView) {
super(itemView);
test = itemView.findViewById(R.id.textMessage);
}
}
@Override
public int getItemCount() {
return chatMessageList.size();
}
@Override
public int getItemViewType(int position) {
return chatMessageList.get(position).getIsSent();
}
static class SentMassageViewHolder extends RecyclerView.ViewHolder {
private final ItemContainerSentMessageBinding binding;
SentMassageViewHolder(ItemContainerSentMessageBinding itemContainerSentMessageBinding) {
super(itemContainerSentMessageBinding.getRoot());
binding = itemContainerSentMessageBinding;
}
void setData(ChatMessage chatMessage) {
binding.textDateTime.setText(chatMessage.getDateTime());
binding.textMessage.setText(chatMessage.getMessage());
}
}
static class ReceivedMassageViewHolder extends RecyclerView.ViewHolder {
private final ItemContainerReceivedMessageBinding binding;
ReceivedMassageViewHolder(ItemContainerReceivedMessageBinding itemContainerReceivedMessageBinding) {
super(itemContainerReceivedMessageBinding.getRoot());
binding = itemContainerReceivedMessageBinding;
}
void setData(ChatMessage chatMessage) {
binding.textDateTime.setText(chatMessage.getDateTime());
binding.textMessage.setText(chatMessage.getMessage());
}
}
}
在
ChatActivity.java
:
import ...
public class ChatActivity extends AppCompatActivity {
private ActivityChatBinding binding;
private List<ChatMessage> chatMessages;
private ChatMessageAdapter chatAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityChatBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
setListeners();
init();
}
private void init() {
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(RecyclerView.VERTICAL);
binding.chatRecycleView.setLayoutManager(manager);
// binding.chatRecycleView.smoothScrollToPosition(0);
initMessageList();
Log.d("In init()", chatMessages.get(0).getMessage());
// binding.chatRecycleView.notify();
chatAdapter = new ChatMessageAdapter(chatMessages);
binding.chatRecycleView.setAdapter(chatAdapter);
}
private void initMessageList() {
chatMessages = new ArrayList<>();
ChatMessage msg1 = new ChatMessage("20231022", "Hello, World!", "Jobs", Constants.VIEW_TYPE_RECEIVED);
chatMessages.add(msg1);
ChatMessage msg2 = new ChatMessage("20231026", "Beep", "Me", Constants.VIEW_TYPE_SENT);
chatMessages.add(msg2);
ChatMessage msg3 = new ChatMessage("20231026", "Don't stop me now", "Jobs", Constants.VIEW_TYPE_RECEIVED);
chatMessages.add(msg3);
}
private void setListeners() {
binding.imageBack.setOnClickListener(v -> finish());
String message = binding.inputText.getText().toString();
binding.send.setOnClickListener(v -> {
if (!message.equals("")) {
chatMessages.add(new ChatMessage("20231029", message, "Me", Constants.VIEW_TYPE_SENT));
}
});
}
}
在
activity_chat.xml
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="@color/primary">
<View
android:id="@+id/viewBackground"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="16dp"
android:background="@drawable/background_content_top"
app:layout_constraintBottom_toTopOf="@id/layoutSend"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/headerBackground"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/primary"
app:layout_constraintBottom_toTopOf="@id/viewSupporter"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageBack"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="12dp"
android:padding="5dp"
android:src="@drawable/ic_back"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:tint="@color/white"/>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageInfo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:padding="4dp"
android:src="@drawable/ic_info"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:tint="@color/white"/>
<TextView
android:id="@+id/textName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:textColor="@color/white"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@id/imageBack"
app:layout_constraintEnd_toStartOf="@id/imageInfo"
app:layout_constraintStart_toEndOf="@id/imageBack"
app:layout_constraintTop_toTopOf="@id/imageBack"/>
<View
android:id="@+id/viewSupporter"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="@id/imageBack" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/chatRecycleView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:clipToPadding="false"
android:orientation="vertical"
android:overScrollMode="never"
android:padding="20dp"
android:visibility="gone"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="@id/viewBackground"
app:layout_constraintTop_toBottomOf="@id/viewSupporter"/>
<FrameLayout
android:id="@+id/layoutSend"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="12dp"
android:background="@drawable/background_chat_input"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:padding="8dp"
android:src="@drawable/ic_send"
app:tint="@color/white" />
</FrameLayout>
<EditText
android:id="@+id/input_text"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="12dp"
android:background="@drawable/background_chat_input"
android:hint="请输入信息"
android:imeOptions="actionDone"
android:importantForAutofill="no"
android:inputType="text"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:textColor="@color/white"
android:textColorHint="#5E83AE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/layoutSend"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在容器中:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="4dp">
<TextView
android:id="@+id/textMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/background_sent_message"
android:padding="12dp"
android:textColor="@color/white"
android:textSize="13sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.8"/>
<TextView
android:id="@+id/textDateTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textColor="@color/secondary_text"
android:textSize="10sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/textMessage"/>
</androidx.constraintlayout.widget.ConstraintLayout>
如您所见,我尝试同时使用
binding.chatRecycleView.smoothScrollToPosition(0);
和binding.chatRecycleView.notify();
但是问题仍然存在。
此外,
Log.d("tag", "why!!!!!!");
中添加的日志onCreateViewHolder()
未触发。这真的让我很困惑。
您能否指导我如何解决此问题并使内容在 RecyclerView 中正确显示?非常感谢!
适配器代码看起来不错,除非我遗漏了一些东西。
我唯一能注意到的是,您已将
RecyclerView
的可见性设置为 gone
。
从您的
android:visibility="gone"
中删除 RecyclerView
。
您还可以将
listitem
属性添加到您的 RecyclerView
。它将在设计预览中显示 RecyclerView
的外观,并避免此类情况。
tools:listitem="@layout/list_item_layout_of_your_reycler_view"