Android ListView条目中的背景图像自动消失

Android ListView条目中的背景图像自动消失,android,listview,imageview,Android,Listview,Imageview,我有一个用自定义布局填充的列表视图 <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/mess

我有一个用自定义布局填充的列表视图

<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>
你可以看到我在imageview上强制设置imagedrawable,但有时在滚动过程中它仍然会消失。有什么想法吗

编辑:

这也是我的listview代码

<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>


尝试在listview上设置
android:cacheColorHint=“#00000000”

尝试设置XML属性

android:scrollingCache="false"
在您的列表视图中。

试试这个

bg.setImageResource(R.drawable.message_bg);

我认为您可以将背景图像绘制到
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
,尝试使用以下代码:

@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;
}
@覆盖
公共视图getView(int位置、视图转换视图、视图组父视图)
{
查看行;
if(null==convertView)
row=mInflater.inflate(R.layout.messagecell,null);
其他的
行=转换视图;
Map message=messageList.get(位置);
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”);
如果(userIcon!=null)
iv.setImageDrawable(用户图标);
其他的
iv.setImageResource(R.drawable.default_photo);
返回行;
}
你的看法是:

<?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>

请尝试使用viewHolder,如以下网站示例所示:

这将帮助你解决这个问题


谢谢,

我知道这是一个简单的答案,但是。。。您在代码中设置BG映像有什么原因吗?你不能试着在xml中设置它吗?它也是在xml中设置的,我也试着将它添加到代码中,只是为了确保它不会在膨胀过程中被覆盖:
ImageView-android:src=“@drawable/message\u-bg”
啊,对不起,我没有发现。我以前在列表视图中遇到过大量的图像问题。我会试试看我是否能找到一个我以前见过的例子
<?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>