在ListView控件项的背景图像消失自发控件、图像、背景、ListView

2023-09-06 05:12:52 作者:善恶都是我

我有,我有一个自定义布局

填充一个ListView

 < RelativeLayout的机器人:ID =@ + ID / relativeLayout1的android:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT的xmlns:机器人=HTTP://模式.android.com / APK / RES / Android的>    < ImageView的机器人:SRC =@绘制/ message_bg    机器人:layout_width =FILL_PARENT    机器人:ID =@ + ID / cellbg    机器人:layout_alignTop =@ + ID / userPhoto    机器人:layout_alignBottom =@ + ID /用户名    机器人:layout_below =@ + ID / userPhoto    机器人:layout_height =FILL_PARENT    机器人:scaleType =centerCrop>< / ImageView的>    < ImageView的机器人:SRC =@绘制/ default_photo    机器人:ID =@ + ID / userPhoto    机器人:layout_alignParentLeft =真    机器人:layout_width =40.0sp    机器人:layout_height =40.0sp    机器人:scaleType =中心>< / ImageView的>    <的TextView的android:文本=TextView的    机器人:ID =@ + ID /消息preVIEW    机器人:TEXTSIZE =20.0sp    机器人:layout_width =WRAP_CONTENT    机器人:layout_height =WRAP_CONTENT    机器人:layout_toRightOf =@ + ID / userPhoto    机器人:layout_alignTop =@ + ID / userPhoto    机器人:行=1>< / TextView的>    <的TextView的android:文本=TextView的    机器人:ID =@ + ID /用户名    机器人:layout_width =WRAP_CONTENT    机器人:layout_height =WRAP_CONTENT    机器人:layout_below =@ + ID /消息preVIEW    机器人:layout_alignLeft =@ + ID /消息preVIEW    机器人:行=1>< / TextView的>< / RelativeLayout的> 

这是图像视图 + @ ID / cellbg 自发消失在我的一些行。我还使用自定义列表适配器:

  @覆盖公共查看getView(INT位置,查看convertView,父母的ViewGroup){    查看排;    如果(空== convertView)    {        行= mInflater.inflate(R.layout.messagecell,NULL);    }    其他    {        排= convertView;    }    地图<字符串,字符串>消息= messageList.get(位置);    TextView的电视=(TextView中)row.findViewById(R.id.username);    //tv.setTextColor(Liveblogging_Main.forColor);    tv.setText(message.get(CreatedBy));    TextView中TV2 =(TextView中)row.findViewById(R.id.message preVIEW);    //tv2.setTextColor(Liveblogging_Main.forColor);    tv2.setText(message.get(描述));    ImageView的IV =(ImageView的)row.findViewById(R.id.userPhoto);    可绘制userIcon = Liveblogging_Main.GetImageForUser(message.get(CreatedBy));    如果(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));    返回行;} 

您可以在那里看到我强行设置上的ImageView的imagedrawable,但它有时在滚动过程中仍然消失。任何想法?

编辑:

怎么给ListView里的每一个列表加图片

下面也是我的列表视图code

 <的LinearLayout机器人:ID =@ + ID / linearLayout1                机器人:layout_width =FILL_PARENT                机器人:layout_height =FILL_PARENT                的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>    < ListView的机器人:ID =@机器人:ID /列表                机器人:layout_width =FILL_PARENT                机器人:layout_height =WRAP_CONTENT                机器人:cacheColorHint =#00000000    >    < /&的ListView GT;< / LinearLayout中> 

解决方案

尝试设置XML属性

 的android:scrollingCache =假 

在您的ListView。

I have a Listview that I am populating with a custom layout

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

That image view +@id/cellbg is spontaneously disappearing in some of my rows. I am also using a custom list adapter:

@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;
}

You can see in there I am forcibly setting the imagedrawable on the imageview, yet it is still disappearing during scrolling sometimes. Any Ideas?

EDIT:

Here is also my listview code

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

解决方案

Try to set XML attribute

android:scrollingCache="false"

in your ListView.