RecyclerView将图像设置在错误的位置

问题描述 投票:3回答:2

我有一个循环视图,2个不同的适配器,我有一个默认的图像。问题是当用户没有图像时,有时它会加载其他用户图像而不是默认图像。 (我还检查用户是否没有imagelink,它以编程方式重置默认图像...所以它应该工作=>文本等没有问题,只有图像!)

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

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rella_rc_item_friend"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="8dp"

card_view:cardBackgroundColor="@color/colorCardView"
card_view:cardCornerRadius="5dp">


<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_marginBottom="10dp"
    android:layout_marginStart="15dp"
    android:layout_marginEnd="15dp"
    card_view:cardBackgroundColor="@color/grey_200"
    card_view:cardCornerRadius="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"

        android:gravity="center">

        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/icon_avata"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/default_avata"
            android:layout_weight="1"
            app:civ_border_color="@color/colorPrimary"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginStart="10dp"
            android:layout_weight="5"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity=""
                android:layout_weight="1"
                android:orientation="horizontal"
                android:paddingTop="5dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                <TextView
                    android:id="@+id/txtName"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:layout_toLeftOf="@id/txtTime"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/txtTime"
                    android:layout_width="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical|right"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                </RelativeLayout>

            </LinearLayout>




            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity=""
                android:layout_weight="1">

                <TextView
                    android:id="@+id/txtMessage"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:ellipsize="end"
                    android:gravity="center_vertical"
                    android:lines="1"
                    android:paddingBottom="10dp"
                    />

            </LinearLayout>


        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

class ListContactAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

public List<MessageInfo> ListContacts;
private Context context;
FirebaseAuth mAuth;
String currentuserid;
SimpleDateFormat DateFormat = new SimpleDateFormat("dd MMMM, yyyy");
SimpleDateFormat TimeFormat = new SimpleDateFormat("hh:mm a");
SimpleDateFormat YearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat DayFormat = new SimpleDateFormat("MMMM dd, hh:mm a");

FusionProject Fobj = new FusionProject();

DatabaseReference VuRef;

CommunActivit obj = new CommunActivit();

public ListContactAdapter(Context context,List<MessageInfo> listGroup){


    this.context = context;
    this.ListContacts = listGroup;
    mAuth = FirebaseAuth.getInstance();
    currentuserid = mAuth.getUid();
    VuRef = FirebaseDatabase.getInstance().getReference().child("Seen");
}
public ListContactAdapter ()
{

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    if (viewType == 1) {

        View view = LayoutInflater.from(context).inflate(R.layout.rc_item_contact_me, parent, false);
        return new ItemForMe(view);
    } else if (viewType == 2) {
        View view = LayoutInflater.from(context).inflate(R.layout.rc_item_friend, parent, false);
        return new ItemForMyFriend(view);
    }

    return null;
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position)

{
    String username = ListContacts.get(position).getFriendUsername();
    String last_message = ListContacts.get(position).getLastMessage();
    String time = ListContacts.get(position).getTime() ;
    String image = ListContacts.get(position).getFriendPicture();
    String FriendID = ListContacts.get(position).getFriendID();
    Date Today = new Date(System.currentTimeMillis());

    long yourmilliseconds = (Long.parseLong(time));

    Date resultdate = new Date(yourmilliseconds);
    String test = DateFormat.format(resultdate).toString(); /**DATE**/
    String test2 = TimeFormat.format(resultdate).toString(); /**TIME**/
    String test3 = YearFormat.format(resultdate);
    String test4 = DayFormat.format(resultdate);



    if (holder instanceof ItemForMe)
    {

        ((ItemForMe) holder).username_friend.setText(username);
        ((ItemForMe) holder).last_message.setText("Me : " +last_message);


        if (DateFormat.format(Today).equals(test))
        {
            ((ItemForMe) holder).time.setText(test2);
        }
        else
        {
            if (YearFormat.format(Today).equals(test3))
            {

                String cap = test4.substring(0, 1).toUpperCase() + test4.substring(1);
                ((ItemForMe) holder).time.setText(cap);
            }
            else {

                ((ItemForMe) holder).time.setText(test);
            }

        }
        if (!image.equals("")) {
            Picasso.get().load(image).into(((ItemForMe) holder).Profil_friend);
        }
        else
            {

            ((ItemForMe) holder).Profil_friend.setImageResource(R.drawable.default_avata);

        }

        VuRef.child(ListContacts.get(position).getIDMessage()).addListenerForSingleValueEvent(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            {
                if (dataSnapshot.exists())
                {
                    if (dataSnapshot.getValue().toString().equals("Vu"))
                    {
                        ((ItemForMe) holder).imageView_vu.setImageDrawable(holder.itemView.getContext().getResources().getDrawable(R.drawable.icons8_seen));
                    }
                    else
                    {
                        ((ItemForMe) holder).imageView_vu.setImageDrawable(holder.itemView.getContext().getResources().getDrawable(R.drawable.icons8_not_vu));
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });



    }
    else
    {


        ((ItemForMyFriend) holder).username_friend.setText(username);
        ((ItemForMyFriend) holder).last_message.setText(last_message);
        if (DateFormat.format(Today).equals(test))
        {
            ((ItemForMyFriend) holder).time.setText(test2);
        }
        else
        {
            if (YearFormat.format(Today).equals(test3))
            {


                String cap = test4.substring(0, 1).toUpperCase() + test4.substring(1);
                ((ItemForMyFriend) holder).time.setText(cap);
            }
            else {

                ((ItemForMyFriend) holder).time.setText(test);
            }

        }

        if (!image.equals("")) {
            Picasso.get().load(image).into(((ItemForMyFriend) holder).Profil_friend);
        }
        else
        {
            ((ItemForMyFriend) holder).Profil_friend.setImageResource(R.drawable.default_avata);
        }

    }

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent groupeIntent = new Intent(v.getContext(), ChatActivity.class);

            Fobj.writefile("FriendID",ListContacts.get(position).getFriendID(), context);
            Fobj.writefile("MessageID",ListContacts.get(position).getIDMessage(), context);
            Fobj.writefile("FriendName",ListContacts.get(position).getFriendUsername(), context);
            Fobj.writefile("FriendPicture",ListContacts.get(position).getFriendPicture(), context);

            obj.setsIDFRIEND(ListContacts.get(position).getFriendID());
            obj.setsIDMESSAGE(ListContacts.get(position).getIDMessage());
            obj.setsFRIENDName(ListContacts.get(position).getFriendUsername());
           context.startActivity(groupeIntent);


        }
    });
}




 @Override
public int getItemViewType(int position) {

    String test = ListContacts.get(position).getSenderID();
    if (test.equals(currentuserid))
    {
        return 1;
    }
    else
        {
            return 2;

    }
}



@Override
public int getItemCount() {
    return ListContacts.size();
}
  }

检索数据:

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

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

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

我已经调试了它似乎有时在持有者中比在列表中更多次。

enter image description here

android android-recyclerview android-imageview android-adapter recyclerview-layout
2个回答
1
投票

您可以像这样尝试Glide。

在app.gradle文件中添加此项

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

像这样滑动取代毕加索

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

1
投票

尝试在您的recyclerview中加载这样的图像

if (!image.trim().isEmpty())    
   Picasso.with(context).load(image)
      .error(R.drawable.default_avata)
      .placeholder(R.drawable.default_avata)
      .into(((ItemForMyFriend) holder).Profil_friend);
© www.soinside.com 2019 - 2024. All rights reserved.