如何修复recyclerview适配器的这种奇怪行为?

时间:2017-01-11 05:49:52

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

我有一些不同架构的消息

{
  "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适配器外观的奇怪行为

enter image description here

一个自定义的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 个答案:

答案 0 :(得分:1)

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

  

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

很高兴它有所帮助:)