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

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

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

这是一个适配器的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,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();

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

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

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

enter image description here

最佳答案
您可以像这样使用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);

猜你在找的Android相关文章