【问题标题】:Background image in ListView entries spontaneously disappearingListView 条目中的背景图像自发消失
【发布时间】:2011-08-29 02:49:27
【问题描述】:

我有一个使用自定义布局填充的 Listview

<RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView android:src="@drawable/message_bg" 
    android:layout_width="fill_parent" 
    android:id="@+id/cellbg" 
    android:layout_alignTop="@+id/userPhoto" 
    android:layout_alignBottom="@+id/username" 
    android:layout_below="@+id/userPhoto" 
    android:layout_height="fill_parent" 
    android:scaleType="centerCrop"></ImageView>

    <ImageView android:src="@drawable/default_photo" 
    android:id="@+id/userPhoto" 
    android:layout_alignParentLeft="true" 
    android:layout_width="40.0sp" 
    android:layout_height="40.0sp" 
    android:scaleType="center"></ImageView>

    <TextView android:text="TextView" 
    android:id="@+id/messagePreview" 
    android:textSize="20.0sp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/userPhoto" 
    android:layout_alignTop="@+id/userPhoto" 
    android:lines="1"></TextView>

    <TextView android:text="TextView" 
    android:id="@+id/username"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/messagePreview" 
    android:layout_alignLeft="@+id/messagePreview" 
    android:lines="1"></TextView>
</RelativeLayout>

该图像视图+@id/cellbg 在我的某些行中自发地消失了。我也在使用自定义列表适配器:

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row;

    if (null == convertView)
    {
        row = mInflater.inflate(R.layout.messagecell, null);
    }
    else
    {
        row = convertView;
    }

    Map<String,String> message = messageList.get(position);

    TextView tv = (TextView) row.findViewById(R.id.username);
    //tv.setTextColor(Liveblogging_Main.forColor);
    tv.setText(message.get("CreatedBy"));

    TextView tv2 = (TextView) row.findViewById(R.id.messagePreview);
    //tv2.setTextColor(Liveblogging_Main.forColor);
    tv2.setText(message.get("Description"));

    ImageView iv = (ImageView) row.findViewById(R.id.userPhoto );
    Drawable userIcon = Liveblogging_Main.GetImageForUser(message.get("CreatedBy"));
    if ( userIcon == null )
    {
        GetUserImage(message.get("CreatedBy"));
        userIcon = getResources().getDrawable(R.drawable.default_photo);
    }
    iv.setImageDrawable(userIcon);
    LayoutParams params = iv.getLayoutParams();
    params.width = 40;
    params.height = 40;
    iv.setLayoutParams(params);
    iv.setScaleType(ScaleType.CENTER_CROP);

    ImageView bg = (ImageView) row.findViewById(R.id.cellbg );
    bg.setImageDrawable(getResources().getDrawable(R.drawable.message_bg));

    return row;
}

您可以在其中看到我在 imageview 上强制设置 imagedrawable,但有时在滚动过程中它仍然会消失。有什么想法吗?

编辑:

这也是我的列表视图代码

<LinearLayout   android:id="@+id/linearLayout1" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                xmlns:android="http://schemas.android.com/apk/res/android">

    <ListView   android:id="@android:id/list" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:cacheColorHint="#00000000"
    >
    </ListView>

</LinearLayout>

【问题讨论】:

  • 我知道这是一个简单的答案,但是......你有什么理由在代码中设置 BG 图像吗?可以不试试在xml里设置吗?
  • 它也在 xml 中设置,我也尝试将它添加到代码中,以确保它不会在通货膨胀的某个地方被覆盖:ImageView android:src="@drawable/message_bg"
  • 啊抱歉 - 没有发现。我以前在列表视图中遇到过很多图像问题。会尝试看看我是否能找到一个我以前见过的例子

标签: android listview imageview


【解决方案1】:

尝试设置 XML 属性

android:scrollingCache="false"

在您的 ListView 中。

【讨论】:

    【解决方案2】:

    尝试在您的列表视图上设置android:cacheColorHint="#00000000"

    【讨论】:

      【解决方案3】:

      试试这个

      bg.setImageResource(R.drawable.message_bg);
      

      【讨论】:

        【解决方案4】:

        我认为您可以将背景图像绘制到RelativeLayout 本身。试试这样的:

        <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/message_bg" xmlns:android="http://schemas.android.com/apk/res/android">
        ...
        

        然后用+@id/cellbg 删除ImageView

        【讨论】:

          【解决方案5】:

          试试这个代码:

          @Override
          public View getView(int position, View convertView, ViewGroup parent)
          {
              View row;
          
              if (null == convertView)
                  row = mInflater.inflate(R.layout.messagecell, null);
              else
                  row = convertView;
          
              Map<String,String> message = messageList.get(position);
          
              TextView tv = (TextView) row.findViewById(R.id.username);
              tv.setText(message.get("CreatedBy"));
          
              TextView tv2 = (TextView) row.findViewById(R.id.messagePreview);
              tv2.setText(message.get("Description"));
          
              ImageView iv = (ImageView) row.findViewById(R.id.userPhoto );
              Drawable userIcon = Liveblogging_Main.GetImageForUser(message.get("CreatedBy"));
              if ( userIcon != null )
                  iv.setImageDrawable(userIcon);
              else
                  iv.setImageResource(R.drawable.default_photo);
          
              return row;
          }
          

          还有你的观点:

          <?xml version="1.0" encoding="utf-8"?>
          
          <RelativeLayout
          android:id="@+id/relativeLayout1"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:background="@drawable/message_bg">
          <ImageView
              android:src="@drawable/default_photo"
              android:id="@+id/userPhoto"
              android:layout_alignParentLeft="true"
              android:layout_width="40.0sp"
              android:layout_height="40.0sp"
              android:scaleType="center"></ImageView>
          
          <TextView
              android:text="TextView"
              android:id="@+id/messagePreview"
              android:textSize="20.0sp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_toRightOf="@+id/userPhoto"
              android:layout_alignTop="@+id/userPhoto"
              android:lines="1"></TextView>
          
          <TextView
              android:text="TextView"
              android:id="@+id/username"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@+id/messagePreview"
              android:layout_alignLeft="@+id/messagePreview"
              android:lines="1"></TextView>
          </RelativeLayout>
          

          【讨论】:

            【解决方案6】:

            请尝试使用以下网站示例中的 viewHolder:

            http://www.anddev.org/view-layout-resource-problems-f27/listview-with-image-and-text-problem-t13694.html

            这将帮助您解决问题。

            谢谢,

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-03-05
              • 1970-01-01
              • 1970-01-01
              • 2016-07-13
              • 1970-01-01
              • 1970-01-01
              • 2011-07-05
              相关资源
              最近更新 更多