英文:
Problem with RecyclerView Adapter in Android App
问题
以下是您提供的代码部分的中文翻译:
// MessageAdapter.java
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageAdapterViewHolder> {
// ... 省略其他代码 ...
}
// UserProfileActivity.java
public class UserProfileActivity extends AppCompatActivity {
// ... 省略其他代码 ...
}
// chat_message_received.xml
<!--
此处为chat_message_received布局的XML内容,省略了一些属性和值。
如果您需要完整的布局内容,请查看原始代码。
-->
// chat_message_sent.xml
<!--
此处为chat_message_sent布局的XML内容,省略了一些属性和值。
如果您需要完整的布局内容,请查看原始代码。
-->
// UserComments Layout:
<!--
此处为UserComments布局的XML内容,省略了具体代码。
您可以在提供的链接中查看完整的布局内容。
-->
// 更新部分
// 问题出在message布局的android:textAppearance属性上,已经将其移除以解决问题。
请注意,由于篇幅较长,上述内容只是代码的主要部分和重点,省略了一些细节。如果您需要特定部分的更多细节,请随时告诉我。
英文:
I'm having a problem with RecyclerView and my adapter. The messages inside the RecyclerView are showing too small in the layout, here you can see in an image: Click to see the Image
MessageAdapter.java:
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageAdapterViewHolder> {
private Context context;
private List<Message> messages;
public static final int MSG_TYPE_LEFT = 0;
public static final int MSG_TYPE_RIGHT = 1;
public MessageAdapter(Context context, List<Message> messages) {
this.context = context;
this.messages = messages;
}
@Override
public int getItemViewType(int position) {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
assert firebaseUser != null;
if (messages.get(position).getName().equals(firebaseUser.getDisplayName())) {
return MSG_TYPE_RIGHT;
} else {
return MSG_TYPE_LEFT;
}
}
@NonNull
@Override
public MessageAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == MSG_TYPE_RIGHT) {
View view = LayoutInflater.from(context).inflate(R.layout.fragment_chat_message_sent, parent, false);
return new MessageAdapterViewHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.fragment_chat_message_received, parent, false);
return new MessageAdapterViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull final MessageAdapterViewHolder holder, final int position) {
final Message message = messages.get(position);
ConstraintLayout constraintLayoutMessageSent;
int viewType = holder.getItemViewType();
if (viewType == MSG_TYPE_LEFT) {
holder.textViewChatName.setText(message.getName());
holder.textViewChatMessage.setText(message.getMessage());
holder.textViewChatMessageDate.setText(message.getHourminute());
holder.imageViewChatUserProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, UserProfileActivity.class);
intent.putExtra("username", message.getName());
context.startActivity(intent);
}
});
} else {
constraintLayoutMessageSent = holder.itemView.findViewById(R.id.constraintLayoutMessageSent);
holder.textViewChatMessage.setText(message.getMessage());
holder.textViewChatMessageDate.setText(message.getHourminute());
holder.imageViewChatMessageStatus.setImageDrawable(ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_message_sent, null));
constraintLayoutMessageSent.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(holder.itemView.getContext());
builder.setMessage(R.string.delete_message)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
deleteMessage(position);
}
}).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();
}
});
builder.create();
builder.show();
return false;
}
});
}
}
private void deleteMessage(int position) {
String messageKey = messages.get(position).getKey();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Messages");
databaseReference.child(messageKey).removeValue();
}
@Override
public int getItemCount() {
return messages.size();
}
public static class MessageAdapterViewHolder extends RecyclerView.ViewHolder {
private TextView textViewChatMessage;
private TextView textViewChatName;
private TextView textViewChatMessageDate;
private ImageView imageViewChatMessageStatus;
private ImageView imageViewChatUserProfile;
public MessageAdapterViewHolder(@NonNull final View itemView) {
super(itemView);
textViewChatName = itemView.findViewById(R.id.textViewChatName);
textViewChatMessage = itemView.findViewById(R.id.textViewChatMessage);
textViewChatMessageDate = itemView.findViewById(R.id.textViewChatMessageDate);
imageViewChatMessageStatus = itemView.findViewById(R.id.imageViewChatMessageStatus);
imageViewChatUserProfile = itemView.findViewById(R.id.imageViewChatUserProfile);
}
}
}
UserCommentsActivity.java:
public class UserProfileActivity extends AppCompatActivity {
private Context context;
private Toolbar toolbar;
private String username = "";
private ImageView imageViewUserProfileProfile;
private TextView textViewUserProfileName;
private TextView textViewUserProfileDescription;
private ConstraintLayout loadingLayout;
private LottieAnimationView lottieAnimationViewLoading;
private RecyclerView recyclerViewUserProfileComments;
private TextView textViewUserProfileNoComments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
username = getIntent().getStringExtra("username");
context = getApplicationContext();
toolbar = findViewById(R.id.toolbarUserProfile);
// TODO set the title of the user profile visiting.
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
bindUI();
}
private void bindUI() {
loadingLayout = findViewById(R.id.loadingLayout);
lottieAnimationViewLoading = findViewById(R.id.lottieAnimationViewLoading);
imageViewUserProfileProfile = findViewById(R.id.imageViewUserProfileProfile);
textViewUserProfileName = findViewById(R.id.textViewUserProfileName);
textViewUserProfileDescription = findViewById(R.id.textViewUserProfileDescription);
recyclerViewUserProfileComments = findViewById(R.id.recyclerViewUserProfileComments);
textViewUserProfileNoComments = findViewById(R.id.textViewUserProfileNoComments);
getUserInformation();
getUserComments();
}
private void getUserInformation() {
loadingLayout.setVisibility(View.VISIBLE);
Thread thread = new Thread() {
@Override
public void run() {
try {
String description = "";
String gender = "";
String profile_picture = "";
Response response = HttpRequests.getUserInformation(username);
String responseBody = Objects.requireNonNull(response.body()).string();
responseBody = responseBody.replace("\r\n", "");
JSONArray responseArray = new JSONArray(responseBody);
for (int i = 0; i < responseArray.length(); i++) {
JSONObject jsonobject = responseArray.getJSONObject(i);
description = jsonobject.getString("description");
gender = jsonobject.getString("gender");
profile_picture = jsonobject.getString("profile_picture");
updateUI(description, gender, profile_picture);
}
} catch (IOException | JSONException e) {
showErrors();
e.printStackTrace();
}
}
};
thread.start();
}
private void getUserComments() {
loadingLayout.setVisibility(View.VISIBLE);
Thread thread = new Thread() {
@Override
public void run() {
try {
List<Message> userMessageList = new ArrayList<>();
String name = "";
String comment = "";
Response response = HttpRequests.getUserComments(username);
String responseBody = Objects.requireNonNull(response.body()).string();
responseBody = responseBody.replace("\r\n", "");
JSONArray responseArray = new JSONArray(responseBody);
for (int i = 0; i < responseArray.length(); i++) {
Message message = new Message();
JSONObject jsonobject = responseArray.getJSONObject(i);
name = jsonobject.getString("from_user");
comment = jsonobject.getString("comment");
if (!name.isEmpty() && !comment.isEmpty()) {
message.setName(name);
message.setMessage(comment);
userMessageList.add(message);
}
}
if (userMessageList.size() > 0) {
updateComments(userMessageList);
}else{
updateCommentSection();
}
} catch (IOException | JSONException e) {
showErrors();
updateCommentSection();
e.printStackTrace();
}
}
};
thread.start();
}
private void showErrors() {
Thread thread = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, getString(R.string.generic_error), Toast.LENGTH_LONG).show();
}
});
}
};
thread.start();
}
private void updateUI(final String description, final String gender, final String pictureURL) {
Thread thread = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
//Picasso.get()
// .load(pictureURL)
// .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
// .into(imageViewUserProfileProfile);
toolbar.setTitle(username + " - " + getString(R.string.user_profile));
textViewUserProfileName.setText(username);
if (description.equals("null")) {
textViewUserProfileDescription.setText("");
} else {
textViewUserProfileDescription.setText(description);
}
loadingLayout.setVisibility(View.GONE);
lottieAnimationViewLoading.cancelAnimation();
}
});
}
};
thread.start();
}
private void updateComments(final List<Message> userMessageList) {
Thread thread = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setStackFromEnd(true);
recyclerViewUserProfileComments.setLayoutManager(linearLayoutManager);
MessageAdapter messageAdapter = new MessageAdapter(context, userMessageList);
recyclerViewUserProfileComments.setAdapter(messageAdapter);
messageAdapter.notifyDataSetChanged();
recyclerViewUserProfileComments.scrollToPosition(userMessageList.size() - 1);
loadingLayout.setVisibility(View.GONE);
}
});
}
};
thread.start();
}
private void updateCommentSection() {
Thread thread = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
recyclerViewUserProfileComments.setVisibility(View.GONE);
textViewUserProfileNoComments.setVisibility(View.VISIBLE);
loadingLayout.setVisibility(View.GONE);
}
});
}
};
thread.start();
}
}
Layouts:
-chat_message_received:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/linearLayoutChatParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="6dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageViewChatUserProfile"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_marginEnd="5dp"
android:background="@drawable/circle"
android:padding="5dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_person_blue" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayoutMessageItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/message_received_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textViewChatName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="@font/among_us_font"
android:padding="5dp"
android:textAppearance="?attr/textAppearanceListItem"
android:textColor="@android:color/black"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textViewChatMessage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Administrator" />
<TextView
android:id="@+id/textViewChatMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginBottom="8dp"
android:fontFamily="@font/among_us_font"
android:padding="5dp"
android:textAppearance="?attr/textAppearanceListItem"
android:textColor="@android:color/black"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textViewChatMessageDate"
app:layout_constraintStart_toStartOf="@+id/textViewChatName"
app:layout_constraintTop_toBottomOf="@+id/textViewChatName"
tools:text="This is a test message This is a test This is a test message" />
<TextView
android:id="@+id/textViewChatMessageDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:fontFamily="@font/among_us_font"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="12:20" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
-chat_message_sent:
<?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:id="@+id/constraintLayoutMessageSent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/message_sent_background"
android:fontFamily="@font/among_us_font"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textViewChatMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="8dp"
android:fontFamily="@font/among_us_font"
android:padding="5dp"
android:textAppearance="?attr/textAppearanceListItem"
android:textColor="@android:color/black"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textViewChatMessageDate"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Test message sent!" />
<TextView
android:id="@+id/textViewChatMessageDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:fontFamily="@font/among_us_font"
app:layout_constraintBottom_toBottomOf="@+id/imageViewChatMessageStatus"
app:layout_constraintEnd_toStartOf="@+id/imageViewChatMessageStatus"
app:layout_constraintStart_toEndOf="@+id/textViewChatMessage"
app:layout_constraintTop_toTopOf="@+id/imageViewChatMessageStatus"
tools:text="12:02" />
<ImageView
android:id="@+id/imageViewChatMessageStatus"
android:layout_width="18sp"
android:layout_height="18sp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_message_waiting" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
-UserComments Layout:
Why is doing that to me? I can't even see the messages.
UPDATE
The problem was the android:textAppearance="?attr/textAppearanceListItem" property in message layouts. I have just removed that and It's working fine.
答案1
得分: 2
可能有两件事情..
1. android:textAppearance="attr/textAppearanceListItem"
2. 字体
尝试移除这些.. 希望这能解决问题。
英文:
There might be 2 things..
1. android:textAppearance="?attr/textAppearanceListItem"
2. fonts
try removing these.. Hope this will solve the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论