Фоновое изображение в записях ListView самопроизвольно исчезает - PullRequest
2 голосов
/ 19 мая 2011

У меня есть 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;
}

Вы можете видеть, что я принудительно устанавливаю изображение для просмотра на изображении, но иногда оно все еще исчезает при прокрутке. Есть идеи?

EDIT:

А вот и мой список просмотра

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

Ответы [ 6 ]

1 голос
/ 24 мая 2011

Попробуйте установить атрибут XML

android:scrollingCache="false"

в вашем ListView.

0 голосов
/ 29 мая 2011

Пожалуйста, попробуйте использовать viewHolder, как показано на следующем примере сайта:

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

Это поможет вам решить проблему.

Спасибо

0 голосов
/ 28 мая 2011

Попробуйте с этим кодом:

@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>
0 голосов
/ 28 мая 2011

Я думаю, вы можете нарисовать фоновое изображение для самого 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">
...

И удалить ImageView с помощью +@id/cellbg.

0 голосов
/ 25 мая 2011

Попробуйте это

bg.setImageResource(R.drawable.message_bg);
0 голосов
/ 19 мая 2011

Попробуйте установить android:cacheColorHint="#00000000" в вашем списке.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...