【问题标题】:How can I fix this strange behavior of recyclerview adapter?如何解决 recyclerview 适配器的这种奇怪行为?
【发布时间】:2017-05-25 19:31:01
【问题描述】:

我有一些具有不同架构的消息

{
  "messages" : {
    "-Ka81zxkKhzi9tTMy3W3" : {
      "messagePhoto" : "https://firebasestorage.googleapis.com/v0/b/MY_APP.appspot.com/o/url_to_image",
      "photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png",
      "title" : "Arshad Ali Soomro",
      "uid" : "USER_ID"
    },
    "-Ka822evCxQ90EQbfUlQ" : {
      "message" : "Hi there",
      "photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png",
      "title" : "Arshad Ali Soomro",
      "uid" : "USER_ID"
    },
    "-Ka82I097uMiD2W1Kwm0" : {
      "message" : "Good Evening every buddy",
      "photoUrl" : "https://lh6.googleusercontent.com/-jSaki6d2OxI/AAAAAAAAAAI/AAAAAAAAAAo/2xwxxl-DEFE/s96-c/photo.jpg",
      "title" : "Arslan Khan Soomro",
      "uid" : "USER_ID"
    }
  }
}

我有这种奇怪的 RecyclerView 适配器外观

一个自定义的 RecyclerView 可以根据我的 xml 布局的逻辑显示文本或图像

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="start"
    android:orientation="horizontal" android:visibility="visible" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <View
        android:id="@+id/them_avatar_spacer"
        android:layout_width="@dimen/avatar_size"
        android:layout_height="0.0dip"
        android:visibility="gone"/>

    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@id/avatar_iv"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_email_account"
        app:civ_border_width="2dp"
        app:civ_border_color="#c6c1c2"
        android:layout_margin="5dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4.0dip"
        android:gravity="start"
        android:orientation="vertical">

        <TextView
            android:id="@id/chat_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2.0dip"
            android:layout_marginLeft="6.0dip"
            android:layout_marginTop="6dp"
            android:includeFontPadding="false"
            android:text="Title"
            android:textColor="@color/black_light"
            android:textSize="@dimen/chat_name_fontsize"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="start"
            android:layout_marginRight="75.0dip"
            android:background="@drawable/selectable_balloon_left"
            android:orientation="vertical">

            <ImageView android:id="@id/chat_msg_img"
                android:layout_width="250dp"
                android:layout_height="250dp"
                android:visibility="gone"/>

            <TextView
                android:id="@id/message_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:autoLink="all"
                android:clickable="true"
                android:lineSpacingExtra="@dimen/text_body_line_spacing"
                android:linksClickable="true"
                android:text="@string/every_message_text_comes_here"
                android:textColor="@color/black_heavy"
                android:textIsSelectable="false"
                android:textSize="@dimen/chat_text_msg_fontsize"/>
        </LinearLayout>

    </LinearLayout>

    <TextView
        android:id="@id/sent_at_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="12:15"
        android:visibility="gone"
        android:textColor="@color/black_lightest"
        android:textSize="@dimen/chat_timestamp_fontsize" />
</LinearLayout>

在适配器中,onBindViewHolder() 是

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    MessageHolder messageHolder = (MessageHolder) holder;
    try{

            if(mMessageList.get(position).getPhotoUrl().equals("")){
                messageHolder.mImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_email_account));
            } else {
                Glide.with(mContext)
                        .load(mMessageList.get(position).getPhotoUrl())
                        .centerCrop()
                        .placeholder(R.drawable.ic_email_account)
                        .crossFade()
                        .into(messageHolder.mImageView);
            }

        if (isPhoto){
            Glide.with(mContext)
                    .load(mMessageList.get(position).getMessagePhoto())
                    .centerCrop()
                    .placeholder(R.drawable.ic_choose_image)
                    .crossFade()
                    .into(messageHolder.mPhotoMessage);
        }

    } catch (Exception e){
        Log.e("ADT 3", "Some thing went wrong...");
        Log.e("ADT ERR", "Cause is " + e.getMessage() + "\n\n" + e.getCause());
    }

        messageHolder.mTitleTextView.setText(mMessageList.get(position).getTitle());
    messageHolder.mMessageTextView.setText(mMessageList.get(position).getMessage());

    try{
        if (!mMessageList.get(position).getMessagePhoto().equals("")){

            Log.e("PHOTO_TAG", "Yes there is photo");

            messageHolder.mChatMsgImg.setVisibility(View.VISIBLE);

            Glide.with(mContext)
                    .load(mMessageList.get(position).getMessagePhoto())
                    .centerCrop()
                    .crossFade()
                    .into(messageHolder.mChatMsgImg);
        } else {
            Log.e("PHOTO_TAG", "No there is no photo");
            messageHolder.mChatMsgImg.setVisibility(View.GONE);
        }
    } catch (Exception e){
        //
        e.printStackTrace();
    }


}

在上面给出的 try 块中,如果它是类型的消息,我想显示图像

-Ka81zxkKhzi9tTMy3W3" : {
      "messagePhoto" : "https://firebasestorage.googleapis.com/v0/b/MY_APP.appspot.com/o/url_to_image",
      "photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png",
      "title" : "Arshad Ali Soomro",
      "uid" : "USER_ID"
    }

如果是消息类型,则显示文本

-Ka822evCxQ90EQbfUlQ" : {
      "message" : "Hi there",
      "photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png",
      "title" : "Arshad Ali Soomro",
      "uid" : "USER_ID"
    }

但正如您所见,图片也显示在

-Ka822evCxQ90EQbfUlQ" : {
      "message" : "Hi there",
      "photoUrl" : "http://pbs.twimg.com/profile_images/666473645494112256/pvJyunCa_normal.png",
      "title" : "Arshad Ali Soomro",
      "uid" : "USER_ID"
    }

如何摆脱这种行为请任何帮助...

【问题讨论】:

  • 哦,伙计!为什么投反对票。 . ?
  • 你努力提出问题并为此制作视频 +1
  • @Redman 非常感谢,但请对此提出任何建议/解决方案,这将是一个拯救生命的...
  • 我不知道是什么导致了这个问题,可能是 glide 正在保存图像缓存
  • 尝试将!mMessageList.get(position).getMessagePhoto().equals("") 更改为!mMessageList.get(position).getMessagePhoto()==null。因为如果 JSON 结构中没有这样的项目,并且您将数据作为对象从 DB 加载,则它将为 null 而不是空字符串。让我知道这是否有帮助。

标签: android firebase firebase-realtime-database android-recyclerview


【解决方案1】:

正如我在 cmets 中所写,问题的解决方案是:

尝试改变 !mMessageList.get(position).getMessagePhoto().equals("") 到 !mMessageList.get(position).getMessagePhoto()==null。因为如果 JSON 结构中没有这样的项目,您正在加载 数据作为来自数据库的对象,它将为空而不是空字符串。让我来 知道这是否有帮助。

很高兴它有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2019-11-10
    • 1970-01-01
    相关资源
    最近更新 更多