在Android应用中的RecyclerView适配器问题

huangapple go评论129阅读模式
英文:

Problem with RecyclerView Adapter in Android App

问题

以下是您提供的代码部分的中文翻译:

  1. // MessageAdapter.java
  2. public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageAdapterViewHolder> {
  3. // ... 省略其他代码 ...
  4. }
  5. // UserProfileActivity.java
  6. public class UserProfileActivity extends AppCompatActivity {
  7. // ... 省略其他代码 ...
  8. }
  9. // chat_message_received.xml
  10. <!--
  11. 此处为chat_message_received布局的XML内容省略了一些属性和值
  12. 如果您需要完整的布局内容请查看原始代码
  13. -->
  14. // chat_message_sent.xml
  15. <!--
  16. 此处为chat_message_sent布局的XML内容省略了一些属性和值
  17. 如果您需要完整的布局内容请查看原始代码
  18. -->
  19. // UserComments Layout:
  20. <!--
  21. 此处为UserComments布局的XML内容省略了具体代码
  22. 您可以在提供的链接中查看完整的布局内容
  23. -->
  24. // 更新部分
  25. // 问题出在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:

  1. public class MessageAdapter extends RecyclerView.Adapter&lt;MessageAdapter.MessageAdapterViewHolder&gt; {
  2. private Context context;
  3. private List&lt;Message&gt; messages;
  4. public static final int MSG_TYPE_LEFT = 0;
  5. public static final int MSG_TYPE_RIGHT = 1;
  6. public MessageAdapter(Context context, List&lt;Message&gt; messages) {
  7. this.context = context;
  8. this.messages = messages;
  9. }
  10. @Override
  11. public int getItemViewType(int position) {
  12. FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
  13. assert firebaseUser != null;
  14. if (messages.get(position).getName().equals(firebaseUser.getDisplayName())) {
  15. return MSG_TYPE_RIGHT;
  16. } else {
  17. return MSG_TYPE_LEFT;
  18. }
  19. }
  20. @NonNull
  21. @Override
  22. public MessageAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  23. if (viewType == MSG_TYPE_RIGHT) {
  24. View view = LayoutInflater.from(context).inflate(R.layout.fragment_chat_message_sent, parent, false);
  25. return new MessageAdapterViewHolder(view);
  26. } else {
  27. View view = LayoutInflater.from(context).inflate(R.layout.fragment_chat_message_received, parent, false);
  28. return new MessageAdapterViewHolder(view);
  29. }
  30. }
  31. @Override
  32. public void onBindViewHolder(@NonNull final MessageAdapterViewHolder holder, final int position) {
  33. final Message message = messages.get(position);
  34. ConstraintLayout constraintLayoutMessageSent;
  35. int viewType = holder.getItemViewType();
  36. if (viewType == MSG_TYPE_LEFT) {
  37. holder.textViewChatName.setText(message.getName());
  38. holder.textViewChatMessage.setText(message.getMessage());
  39. holder.textViewChatMessageDate.setText(message.getHourminute());
  40. holder.imageViewChatUserProfile.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. Intent intent = new Intent(context, UserProfileActivity.class);
  44. intent.putExtra(&quot;username&quot;, message.getName());
  45. context.startActivity(intent);
  46. }
  47. });
  48. } else {
  49. constraintLayoutMessageSent = holder.itemView.findViewById(R.id.constraintLayoutMessageSent);
  50. holder.textViewChatMessage.setText(message.getMessage());
  51. holder.textViewChatMessageDate.setText(message.getHourminute());
  52. holder.imageViewChatMessageStatus.setImageDrawable(ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_message_sent, null));
  53. constraintLayoutMessageSent.setOnLongClickListener(new View.OnLongClickListener() {
  54. @Override
  55. public boolean onLongClick(View v) {
  56. AlertDialog.Builder builder = new AlertDialog.Builder(holder.itemView.getContext());
  57. builder.setMessage(R.string.delete_message)
  58. .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
  59. @Override
  60. public void onClick(DialogInterface dialogInterface, int i) {
  61. deleteMessage(position);
  62. }
  63. }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
  64. @Override
  65. public void onClick(DialogInterface dialogInterface, int which) {
  66. dialogInterface.dismiss();
  67. }
  68. });
  69. builder.create();
  70. builder.show();
  71. return false;
  72. }
  73. });
  74. }
  75. }
  76. private void deleteMessage(int position) {
  77. String messageKey = messages.get(position).getKey();
  78. DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference(&quot;Messages&quot;);
  79. databaseReference.child(messageKey).removeValue();
  80. }
  81. @Override
  82. public int getItemCount() {
  83. return messages.size();
  84. }
  85. public static class MessageAdapterViewHolder extends RecyclerView.ViewHolder {
  86. private TextView textViewChatMessage;
  87. private TextView textViewChatName;
  88. private TextView textViewChatMessageDate;
  89. private ImageView imageViewChatMessageStatus;
  90. private ImageView imageViewChatUserProfile;
  91. public MessageAdapterViewHolder(@NonNull final View itemView) {
  92. super(itemView);
  93. textViewChatName = itemView.findViewById(R.id.textViewChatName);
  94. textViewChatMessage = itemView.findViewById(R.id.textViewChatMessage);
  95. textViewChatMessageDate = itemView.findViewById(R.id.textViewChatMessageDate);
  96. imageViewChatMessageStatus = itemView.findViewById(R.id.imageViewChatMessageStatus);
  97. imageViewChatUserProfile = itemView.findViewById(R.id.imageViewChatUserProfile);
  98. }
  99. }
  100. }

UserCommentsActivity.java:

  1. public class UserProfileActivity extends AppCompatActivity {
  2. private Context context;
  3. private Toolbar toolbar;
  4. private String username = &quot;&quot;;
  5. private ImageView imageViewUserProfileProfile;
  6. private TextView textViewUserProfileName;
  7. private TextView textViewUserProfileDescription;
  8. private ConstraintLayout loadingLayout;
  9. private LottieAnimationView lottieAnimationViewLoading;
  10. private RecyclerView recyclerViewUserProfileComments;
  11. private TextView textViewUserProfileNoComments;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_user_profile);
  16. username = getIntent().getStringExtra(&quot;username&quot;);
  17. context = getApplicationContext();
  18. toolbar = findViewById(R.id.toolbarUserProfile);
  19. // TODO set the title of the user profile visiting.
  20. setSupportActionBar(toolbar);
  21. Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
  22. getSupportActionBar().setDisplayShowHomeEnabled(true);
  23. toolbar.setNavigationOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. onBackPressed();
  27. }
  28. });
  29. bindUI();
  30. }
  31. private void bindUI() {
  32. loadingLayout = findViewById(R.id.loadingLayout);
  33. lottieAnimationViewLoading = findViewById(R.id.lottieAnimationViewLoading);
  34. imageViewUserProfileProfile = findViewById(R.id.imageViewUserProfileProfile);
  35. textViewUserProfileName = findViewById(R.id.textViewUserProfileName);
  36. textViewUserProfileDescription = findViewById(R.id.textViewUserProfileDescription);
  37. recyclerViewUserProfileComments = findViewById(R.id.recyclerViewUserProfileComments);
  38. textViewUserProfileNoComments = findViewById(R.id.textViewUserProfileNoComments);
  39. getUserInformation();
  40. getUserComments();
  41. }
  42. private void getUserInformation() {
  43. loadingLayout.setVisibility(View.VISIBLE);
  44. Thread thread = new Thread() {
  45. @Override
  46. public void run() {
  47. try {
  48. String description = &quot;&quot;;
  49. String gender = &quot;&quot;;
  50. String profile_picture = &quot;&quot;;
  51. Response response = HttpRequests.getUserInformation(username);
  52. String responseBody = Objects.requireNonNull(response.body()).string();
  53. responseBody = responseBody.replace(&quot;\r\n&quot;, &quot;&quot;);
  54. JSONArray responseArray = new JSONArray(responseBody);
  55. for (int i = 0; i &lt; responseArray.length(); i++) {
  56. JSONObject jsonobject = responseArray.getJSONObject(i);
  57. description = jsonobject.getString(&quot;description&quot;);
  58. gender = jsonobject.getString(&quot;gender&quot;);
  59. profile_picture = jsonobject.getString(&quot;profile_picture&quot;);
  60. updateUI(description, gender, profile_picture);
  61. }
  62. } catch (IOException | JSONException e) {
  63. showErrors();
  64. e.printStackTrace();
  65. }
  66. }
  67. };
  68. thread.start();
  69. }
  70. private void getUserComments() {
  71. loadingLayout.setVisibility(View.VISIBLE);
  72. Thread thread = new Thread() {
  73. @Override
  74. public void run() {
  75. try {
  76. List&lt;Message&gt; userMessageList = new ArrayList&lt;&gt;();
  77. String name = &quot;&quot;;
  78. String comment = &quot;&quot;;
  79. Response response = HttpRequests.getUserComments(username);
  80. String responseBody = Objects.requireNonNull(response.body()).string();
  81. responseBody = responseBody.replace(&quot;\r\n&quot;, &quot;&quot;);
  82. JSONArray responseArray = new JSONArray(responseBody);
  83. for (int i = 0; i &lt; responseArray.length(); i++) {
  84. Message message = new Message();
  85. JSONObject jsonobject = responseArray.getJSONObject(i);
  86. name = jsonobject.getString(&quot;from_user&quot;);
  87. comment = jsonobject.getString(&quot;comment&quot;);
  88. if (!name.isEmpty() &amp;&amp; !comment.isEmpty()) {
  89. message.setName(name);
  90. message.setMessage(comment);
  91. userMessageList.add(message);
  92. }
  93. }
  94. if (userMessageList.size() &gt; 0) {
  95. updateComments(userMessageList);
  96. }else{
  97. updateCommentSection();
  98. }
  99. } catch (IOException | JSONException e) {
  100. showErrors();
  101. updateCommentSection();
  102. e.printStackTrace();
  103. }
  104. }
  105. };
  106. thread.start();
  107. }
  108. private void showErrors() {
  109. Thread thread = new Thread() {
  110. public void run() {
  111. runOnUiThread(new Runnable() {
  112. public void run() {
  113. Toast.makeText(context, getString(R.string.generic_error), Toast.LENGTH_LONG).show();
  114. }
  115. });
  116. }
  117. };
  118. thread.start();
  119. }
  120. private void updateUI(final String description, final String gender, final String pictureURL) {
  121. Thread thread = new Thread() {
  122. public void run() {
  123. runOnUiThread(new Runnable() {
  124. public void run() {
  125. //Picasso.get()
  126. // .load(pictureURL)
  127. // .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
  128. // .into(imageViewUserProfileProfile);
  129. toolbar.setTitle(username + &quot; - &quot; + getString(R.string.user_profile));
  130. textViewUserProfileName.setText(username);
  131. if (description.equals(&quot;null&quot;)) {
  132. textViewUserProfileDescription.setText(&quot;&quot;);
  133. } else {
  134. textViewUserProfileDescription.setText(description);
  135. }
  136. loadingLayout.setVisibility(View.GONE);
  137. lottieAnimationViewLoading.cancelAnimation();
  138. }
  139. });
  140. }
  141. };
  142. thread.start();
  143. }
  144. private void updateComments(final List&lt;Message&gt; userMessageList) {
  145. Thread thread = new Thread() {
  146. public void run() {
  147. runOnUiThread(new Runnable() {
  148. public void run() {
  149. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
  150. linearLayoutManager.setStackFromEnd(true);
  151. recyclerViewUserProfileComments.setLayoutManager(linearLayoutManager);
  152. MessageAdapter messageAdapter = new MessageAdapter(context, userMessageList);
  153. recyclerViewUserProfileComments.setAdapter(messageAdapter);
  154. messageAdapter.notifyDataSetChanged();
  155. recyclerViewUserProfileComments.scrollToPosition(userMessageList.size() - 1);
  156. loadingLayout.setVisibility(View.GONE);
  157. }
  158. });
  159. }
  160. };
  161. thread.start();
  162. }
  163. private void updateCommentSection() {
  164. Thread thread = new Thread() {
  165. public void run() {
  166. runOnUiThread(new Runnable() {
  167. public void run() {
  168. recyclerViewUserProfileComments.setVisibility(View.GONE);
  169. textViewUserProfileNoComments.setVisibility(View.VISIBLE);
  170. loadingLayout.setVisibility(View.GONE);
  171. }
  172. });
  173. }
  174. };
  175. thread.start();
  176. }
  177. }

Layouts:

-chat_message_received:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:id=&quot;@+id/linearLayoutChatParent&quot;
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;wrap_content&quot;
  8. android:layout_marginStart=&quot;16dp&quot;
  9. android:layout_marginTop=&quot;6dp&quot;
  10. android:layout_marginEnd=&quot;16dp&quot;
  11. android:layout_marginBottom=&quot;6dp&quot;
  12. android:orientation=&quot;horizontal&quot;&gt;
  13. &lt;ImageView
  14. android:id=&quot;@+id/imageViewChatUserProfile&quot;
  15. android:layout_width=&quot;50sp&quot;
  16. android:layout_height=&quot;50sp&quot;
  17. android:layout_marginEnd=&quot;5dp&quot;
  18. android:background=&quot;@drawable/circle&quot;
  19. android:padding=&quot;5dp&quot;
  20. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  21. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  22. app:srcCompat=&quot;@drawable/ic_person_blue&quot; /&gt;
  23. &lt;androidx.constraintlayout.widget.ConstraintLayout
  24. android:id=&quot;@+id/constraintLayoutMessageItem&quot;
  25. android:layout_width=&quot;wrap_content&quot;
  26. android:layout_height=&quot;wrap_content&quot;
  27. android:background=&quot;@drawable/message_received_background&quot;
  28. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  29. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  30. app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
  31. &lt;TextView
  32. android:id=&quot;@+id/textViewChatName&quot;
  33. android:layout_width=&quot;wrap_content&quot;
  34. android:layout_height=&quot;wrap_content&quot;
  35. android:layout_marginStart=&quot;8dp&quot;
  36. android:layout_marginTop=&quot;8dp&quot;
  37. android:fontFamily=&quot;@font/among_us_font&quot;
  38. android:padding=&quot;5dp&quot;
  39. android:textAppearance=&quot;?attr/textAppearanceListItem&quot;
  40. android:textColor=&quot;@android:color/black&quot;
  41. android:textStyle=&quot;bold&quot;
  42. app:layout_constraintBottom_toTopOf=&quot;@+id/textViewChatMessage&quot;
  43. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  44. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  45. tools:text=&quot;Administrator&quot; /&gt;
  46. &lt;TextView
  47. android:id=&quot;@+id/textViewChatMessage&quot;
  48. android:layout_width=&quot;wrap_content&quot;
  49. android:layout_height=&quot;wrap_content&quot;
  50. android:layout_marginEnd=&quot;4dp&quot;
  51. android:layout_marginBottom=&quot;8dp&quot;
  52. android:fontFamily=&quot;@font/among_us_font&quot;
  53. android:padding=&quot;5dp&quot;
  54. android:textAppearance=&quot;?attr/textAppearanceListItem&quot;
  55. android:textColor=&quot;@android:color/black&quot;
  56. app:layout_constrainedWidth=&quot;true&quot;
  57. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  58. app:layout_constraintEnd_toStartOf=&quot;@+id/textViewChatMessageDate&quot;
  59. app:layout_constraintStart_toStartOf=&quot;@+id/textViewChatName&quot;
  60. app:layout_constraintTop_toBottomOf=&quot;@+id/textViewChatName&quot;
  61. tools:text=&quot;This is a test message This is a test This is a test message&quot; /&gt;
  62. &lt;TextView
  63. android:id=&quot;@+id/textViewChatMessageDate&quot;
  64. android:layout_width=&quot;wrap_content&quot;
  65. android:layout_height=&quot;wrap_content&quot;
  66. android:layout_marginEnd=&quot;8dp&quot;
  67. android:fontFamily=&quot;@font/among_us_font&quot;
  68. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  69. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  70. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  71. tools:text=&quot;12:20&quot; /&gt;
  72. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  73. &lt;/LinearLayout&gt;

-chat_message_sent:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:id=&quot;@+id/constraintLayoutMessageSent&quot;
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;wrap_content&quot;
  8. android:layout_marginStart=&quot;16dp&quot;
  9. android:layout_marginTop=&quot;8dp&quot;
  10. android:layout_marginEnd=&quot;16dp&quot;
  11. android:layout_marginBottom=&quot;8dp&quot;&gt;
  12. &lt;androidx.constraintlayout.widget.ConstraintLayout
  13. android:layout_width=&quot;wrap_content&quot;
  14. android:layout_height=&quot;wrap_content&quot;
  15. android:background=&quot;@drawable/message_sent_background&quot;
  16. android:fontFamily=&quot;@font/among_us_font&quot;
  17. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  18. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  19. app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
  20. &lt;TextView
  21. android:id=&quot;@+id/textViewChatMessage&quot;
  22. android:layout_width=&quot;wrap_content&quot;
  23. android:layout_height=&quot;wrap_content&quot;
  24. android:layout_marginStart=&quot;8dp&quot;
  25. android:layout_marginTop=&quot;8dp&quot;
  26. android:layout_marginEnd=&quot;4dp&quot;
  27. android:layout_marginBottom=&quot;8dp&quot;
  28. android:fontFamily=&quot;@font/among_us_font&quot;
  29. android:padding=&quot;5dp&quot;
  30. android:textAppearance=&quot;?attr/textAppearanceListItem&quot;
  31. android:textColor=&quot;@android:color/black&quot;
  32. app:layout_constrainedWidth=&quot;true&quot;
  33. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  34. app:layout_constraintEnd_toStartOf=&quot;@+id/textViewChatMessageDate&quot;
  35. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  36. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  37. tools:text=&quot;Test message sent!&quot; /&gt;
  38. &lt;TextView
  39. android:id=&quot;@+id/textViewChatMessageDate&quot;
  40. android:layout_width=&quot;wrap_content&quot;
  41. android:layout_height=&quot;wrap_content&quot;
  42. android:layout_marginEnd=&quot;4dp&quot;
  43. android:fontFamily=&quot;@font/among_us_font&quot;
  44. app:layout_constraintBottom_toBottomOf=&quot;@+id/imageViewChatMessageStatus&quot;
  45. app:layout_constraintEnd_toStartOf=&quot;@+id/imageViewChatMessageStatus&quot;
  46. app:layout_constraintStart_toEndOf=&quot;@+id/textViewChatMessage&quot;
  47. app:layout_constraintTop_toTopOf=&quot;@+id/imageViewChatMessageStatus&quot;
  48. tools:text=&quot;12:02&quot; /&gt;
  49. &lt;ImageView
  50. android:id=&quot;@+id/imageViewChatMessageStatus&quot;
  51. android:layout_width=&quot;18sp&quot;
  52. android:layout_height=&quot;18sp&quot;
  53. android:layout_marginTop=&quot;8dp&quot;
  54. android:layout_marginEnd=&quot;8dp&quot;
  55. android:layout_marginBottom=&quot;8dp&quot;
  56. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  57. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  58. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  59. app:srcCompat=&quot;@drawable/ic_message_waiting&quot; /&gt;
  60. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  61. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

-UserComments Layout:

Code Link

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.

huangapple
  • 本文由 发表于 2020年10月26日 22:27:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64539081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定