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

发布于 2024-11-08 11:57:54 字数 3534 浏览 0 评论 0原文

我有一个列表视图,我正在使用自定义布局填充

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜心 2024-11-15 11:57:54

设置 XML 属性。

android:scrollingCache="false"

尝试在 ListView 中

Try to set XML attribute

android:scrollingCache="false"

in your ListView.

蝶…霜飞 2024-11-15 11:57:54

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

Try setting android:cacheColorHint="#00000000" on your listview.

榕城若虚 2024-11-15 11:57:54

试试这个

bg.setImageResource(R.drawable.message_bg);

Try this

bg.setImageResource(R.drawable.message_bg);
萤火眠眠 2024-11-15 11:57:54

我认为您可以将背景图像绘制到 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

I think you can draw the background image to the RelativeLayout itself. Try something like this:

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

And remove the ImageView with +@id/cellbg.

垂暮老矣 2024-11-15 11:57:54

尝试使用以下代码:

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

Try with this code:

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

And your View:

<?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>
如何视而不见 2024-11-15 11:57:54

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

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

这将帮助您解决问题。

谢谢,

Please, try to use viewHolder like in below website example :

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

This will help you to solve the problem.

Thanks,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文