android-RecyclerView在错误的位置设置图像

前端之家收集整理的这篇文章主要介绍了android-RecyclerView在错误的位置设置图像 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个回收站视图,有2种适配器,并且有一个默认的Image.问题在于,当用户没有图像时,有时会加载其他用户图像而不是默认图像.
(我还会检查用户是否没有图像链接,它会以编程方式重置默认图像…,所以它应该可以工作=>文本等没有问题,只有图像!)

这是一个适配器的xml代码(对于另一个适配器几乎相同):

  1. <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
  2. android:id="@+id/rella_rc_item_friend"
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:card_view="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:layout_marginBottom="5dp"
  8. android:layout_marginTop="8dp"
  9. card_view:cardBackgroundColor="@color/colorCardView"
  10. card_view:cardCornerRadius="5dp">
  11. <android.support.v7.widget.CardView
  12. android:layout_width="match_parent"
  13. android:layout_height="70dp"
  14. android:layout_marginBottom="10dp"
  15. android:layout_marginStart="15dp"
  16. android:layout_marginEnd="15dp"
  17. card_view:cardBackgroundColor="@color/grey_200"
  18. card_view:cardCornerRadius="5dp">
  19. <LinearLayout
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent"
  22. android:orientation="horizontal"
  23. android:gravity="center">
  24. <de.hdodenhof.circleimageview.CircleImageView
  25. xmlns:app="http://schemas.android.com/apk/res-auto"
  26. android:id="@+id/icon_avata"
  27. android:layout_width="0dp"
  28. android:layout_height="50dp"
  29. android:layout_margin="10dp"
  30. android:src="@drawable/default_avata"
  31. android:layout_weight="1"
  32. app:civ_border_color="@color/colorPrimary"/>
  33. <LinearLayout
  34. android:layout_width="0dp"
  35. android:layout_height="match_parent"
  36. android:layout_marginStart="10dp"
  37. android:layout_weight="5"
  38. android:orientation="vertical">
  39. <LinearLayout
  40. android:layout_width="match_parent"
  41. android:layout_height="0dp"
  42. android:layout_gravity=""
  43. android:layout_weight="1"
  44. android:orientation="horizontal"
  45. android:paddingTop="5dp">
  46. <RelativeLayout
  47. android:layout_width="match_parent"
  48. android:layout_height="match_parent">
  49. <TextView
  50. android:id="@+id/txtName"
  51. android:layout_width="match_parent"
  52. android:layout_height="match_parent"
  53. android:gravity="center_vertical"
  54. android:layout_toLeftOf="@id/txtTime"
  55. android:textAppearance="?android:attr/textAppearanceMedium" />
  56. <TextView
  57. android:id="@+id/txtTime"
  58. android:layout_width="wrap_content"
  59. android:layout_alignParentRight="true"
  60. android:layout_height="match_parent"
  61. android:gravity="center_vertical|right"
  62. android:textAppearance="?android:attr/textAppearanceSmall" />
  63. </RelativeLayout>
  64. </LinearLayout>
  65. <LinearLayout
  66. android:layout_width="match_parent"
  67. android:layout_height="0dp"
  68. android:layout_gravity=""
  69. android:layout_weight="1">
  70. <TextView
  71. android:id="@+id/txtMessage"
  72. android:layout_width="match_parent"
  73. android:layout_height="match_parent"
  74. android:ellipsize="end"
  75. android:gravity="center_vertical"
  76. android:lines="1"
  77. android:paddingBottom="10dp"
  78. />
  79. </LinearLayout>
  80. </LinearLayout>
  81. </LinearLayout>
  82. </android.support.v7.widget.CardView>
  1. class ListContactAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  2. public List<MessageInfo> ListContacts;
  3. private Context context;
  4. FirebaseAuth mAuth;
  5. String currentuserid;
  6. SimpleDateFormat DateFormat = new SimpleDateFormat("dd MMMM,yyyy");
  7. SimpleDateFormat TimeFormat = new SimpleDateFormat("hh:mm a");
  8. SimpleDateFormat YearFormat = new SimpleDateFormat("yyyy");
  9. SimpleDateFormat DayFormat = new SimpleDateFormat("MMMM dd,hh:mm a");
  10. FusionProject Fobj = new FusionProject();
  11. DatabaseReference VuRef;
  12. CommunActivit obj = new CommunActivit();
  13. public ListContactAdapter(Context context,List<MessageInfo> listGroup){
  14. this.context = context;
  15. this.ListContacts = listGroup;
  16. mAuth = FirebaseAuth.getInstance();
  17. currentuserid = mAuth.getUid();
  18. VuRef = FirebaseDatabase.getInstance().getReference().child("Seen");
  19. }
  20. public ListContactAdapter ()
  21. {
  22. }
  23. @Override
  24. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
  25. if (viewType == 1) {
  26. View view = LayoutInflater.from(context).inflate(R.layout.rc_item_contact_me,parent,false);
  27. return new ItemForMe(view);
  28. } else if (viewType == 2) {
  29. View view = LayoutInflater.from(context).inflate(R.layout.rc_item_friend,false);
  30. return new ItemForMyFriend(view);
  31. }
  32. return null;
  33. }
  34. @Override
  35. public void onBindViewHolder(final RecyclerView.ViewHolder holder,final int position)
  36. {
  37. String username = ListContacts.get(position).getFriendUsername();
  38. String last_message = ListContacts.get(position).getLastMessage();
  39. String time = ListContacts.get(position).getTime() ;
  40. String image = ListContacts.get(position).getFriendPicture();
  41. String FriendID = ListContacts.get(position).getFriendID();
  42. Date Today = new Date(System.currentTimeMillis());
  43. long yourmilliseconds = (Long.parseLong(time));
  44. Date resultdate = new Date(yourmilliseconds);
  45. String test = DateFormat.format(resultdate).toString(); /**DATE**/
  46. String test2 = TimeFormat.format(resultdate).toString(); /**TIME**/
  47. String test3 = YearFormat.format(resultdate);
  48. String test4 = DayFormat.format(resultdate);
  49. if (holder instanceof ItemForMe)
  50. {
  51. ((ItemForMe) holder).username_friend.setText(username);
  52. ((ItemForMe) holder).last_message.setText("Me : " +last_message);
  53. if (DateFormat.format(Today).equals(test))
  54. {
  55. ((ItemForMe) holder).time.setText(test2);
  56. }
  57. else
  58. {
  59. if (YearFormat.format(Today).equals(test3))
  60. {
  61. String cap = test4.substring(0,1).toUpperCase() + test4.substring(1);
  62. ((ItemForMe) holder).time.setText(cap);
  63. }
  64. else {
  65. ((ItemForMe) holder).time.setText(test);
  66. }
  67. }
  68. if (!image.equals("")) {
  69. Picasso.get().load(image).into(((ItemForMe) holder).Profil_friend);
  70. }
  71. else
  72. {
  73. ((ItemForMe) holder).Profil_friend.setImageResource(R.drawable.default_avata);
  74. }
  75. VuRef.child(ListContacts.get(position).getIDMessage()).addListenerForSingleValueEvent(new ValueEventListener()
  76. {
  77. @Override
  78. public void onDataChange(@NonNull DataSnapshot dataSnapshot)
  79. {
  80. if (dataSnapshot.exists())
  81. {
  82. if (dataSnapshot.getValue().toString().equals("Vu"))
  83. {
  84. ((ItemForMe) holder).imageView_vu.setImageDrawable(holder.itemView.getContext().getResources().getDrawable(R.drawable.icons8_seen));
  85. }
  86. else
  87. {
  88. ((ItemForMe) holder).imageView_vu.setImageDrawable(holder.itemView.getContext().getResources().getDrawable(R.drawable.icons8_not_vu));
  89. }
  90. }
  91. }
  92. @Override
  93. public void onCancelled(@NonNull DatabaseError databaseError) {
  94. }
  95. });
  96. }
  97. else
  98. {
  99. ((ItemForMyFriend) holder).username_friend.setText(username);
  100. ((ItemForMyFriend) holder).last_message.setText(last_message);
  101. if (DateFormat.format(Today).equals(test))
  102. {
  103. ((ItemForMyFriend) holder).time.setText(test2);
  104. }
  105. else
  106. {
  107. if (YearFormat.format(Today).equals(test3))
  108. {
  109. String cap = test4.substring(0,1).toUpperCase() + test4.substring(1);
  110. ((ItemForMyFriend) holder).time.setText(cap);
  111. }
  112. else {
  113. ((ItemForMyFriend) holder).time.setText(test);
  114. }
  115. }
  116. if (!image.equals("")) {
  117. Picasso.get().load(image).into(((ItemForMyFriend) holder).Profil_friend);
  118. }
  119. else
  120. {
  121. ((ItemForMyFriend) holder).Profil_friend.setImageResource(R.drawable.default_avata);
  122. }
  123. }
  124. holder.itemView.setOnClickListener(new View.OnClickListener() {
  125. @Override
  126. public void onClick(View v) {
  127. Intent groupeIntent = new Intent(v.getContext(),ChatActivity.class);
  128. Fobj.writefile("FriendID",ListContacts.get(position).getFriendID(),context);
  129. Fobj.writefile("MessageID",ListContacts.get(position).getIDMessage(),context);
  130. Fobj.writefile("FriendName",ListContacts.get(position).getFriendUsername(),context);
  131. Fobj.writefile("FriendPicture",ListContacts.get(position).getFriendPicture(),context);
  132. obj.setsIDFRIEND(ListContacts.get(position).getFriendID());
  133. obj.setsIDMESSAGE(ListContacts.get(position).getIDMessage());
  134. obj.setsFRIENDName(ListContacts.get(position).getFriendUsername());
  135. context.startActivity(groupeIntent);
  136. }
  137. });
  138. }
  139. @Override
  140. public int getItemViewType(int position) {
  141. String test = ListContacts.get(position).getSenderID();
  142. if (test.equals(currentuserid))
  143. {
  144. return 1;
  145. }
  146. else
  147. {
  148. return 2;
  149. }
  150. }
  151. @Override
  152. public int getItemCount() {
  153. return ListContacts.size();
  154. }
  155. }

检索数据:

  1. List_of_friends.add(0,new MessageInfo(ID,newgroup,profilepicture,MessageID,Date,Message,Time,SenderID));
  2. adapter.notifyDataSetChanged();

因此,您可以在此图像上看到,第一条消息未正确加载.该用户没有图片,但获得了另一个用户图片用户

如果有人可以帮助,那就太好了!

我已经调试过了,看来它有时在固定器中的移动次数比在listsize中的移动次数更多.

enter image description here

最佳答案
您可以像这样使用Glide.

将此添加到app.gradle文件

  1. implementation "com.github.bumptech.glide:glide:4.8.0"
  2. annotationProcessor "com.github.bumptech.glide:compiler:4.8.0"

像这样用滑行代替毕加索

  1. if (!image.trim().isEmpty())
  2. Glide.with(context).load(image).into(((ItemForMyFriend) holder).Profil_friend);

猜你在找的Android相关文章