Android ExpandableListView 项目图像消失

Android ExpandableListView item images disappear

我必须在列表中显示足球运动员列表。没问题,一切正常,但当我向下滚动列表时,国籍的小旗帜正在消失。

它应该是这样的:list item player OK
这是滚动后的结果:list item player NOK

我还有一张球员肖像图片和另一张展示球衣的图片,效果很好。

这是我的代码的一部分(我只放了其中的一部分以减小大小并放置有效的部分,但如果需要,请随时询问更多):

我的项目列表布局(部分布局,你不会得到与图像显示相同的结果):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="3dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="3dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/portrait"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:background="@drawable/portrait_default" />

    <LinearLayout
        android:id="@+id/nationalityLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/logoNationality"
            android:layout_width="13dp"
            android:layout_height="13dp"
            android:layout_marginRight="3dp" />

        <TextView
            android:id="@+id/nationality"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="13dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/shirt"
        android:layout_width="40dp"
        android:layout_height="32dp"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp" />
    </LinearLayout>
</LinearLayout>

我的物品有一个支架 :

private class ChildViewHolder {
    public ImageView portrait;
    public ImageView logoNationality;
    public TextView nationality;
    public LinearLayout nationalityLayout;
    public LinearLayout shirt;
    public TextView number;

    public void reset() {
        portrait.setImageBitmap(null);
        portrait.setBackgroundResource(R.drawable.portrait_default);
        nationalityLayout.setVisibility(View.VISIBLE);
        nationality.setText("");
        number.setText("");
    }
}

我的适配器中的 getChildView(标记是在将项目放入适配器列表时填充的地图,之前加载的是 BitmapDrawable 的 playerShirt):

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ChildViewHolder holder;

    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.liste_item, null);
        holder = new ChildViewHolder();
        holder.portrait = (ImageView) convertView.findViewById(R.id.portrait);
        holder.nationalityLayout = (LinearLayout) convertView.findViewById(R.id.nationalityLayout);
        holder.logoNationality = (ImageView) convertView.findViewById(R.id.logoNationality);
        holder.nationality = (TextView) convertView.findViewById(R.id.nationality);
        holder.shirt= (LinearLayout) convertView.findViewById(R.id.shirt);
        holder.number = (TextView) convertView.findViewById(R.id.number);

        convertView.setTag(holder);
    } else {
        holder = (ChildViewHolder) convertView.getTag();
        holder.reset();
    }

    FootPlayer player = children.get(groupPosition).get(childPosition);

    // Doing nothing with portrait for now
    if (player.getNationality() != null) {
        holder.nationalityLayout.setVisibility(View.VISIBLE);
        holder.nationality.setText(player.getNationality());
        if (player.getNatImg() != null) {
            holder.logoNationality.setImageDrawable(flags.get(player.getNatImg()));
        } else {
            holder.logoNationality.setVisibility(View.INVISIBLE);
        }
    } else {
        holder.nationalityLayout.setVisibility(View.GONE);
    }
    holder.shirt.setBackgroundDrawable(playerShirt);
    holder.number.setText(String.valueOf(player.getNumber()));

    return convertView;
}

现在,旗帜出现在列表的开头,证明旗帜地图正确填充了我的图像,但当我向下滚动时,它们开始随机消失,最后 none他们中的一个出现了。

所以重点是:

非常感谢有关此问题的任何帮助。

您没有像设置 nationalityLayout 那样将 logoNationality 可见性设置回 VISIBLE。所以我想一旦它第一次消失,它就会保持不可见,即使您稍后重新使用视图设置可绘制对象也是如此。

查看固定代码:

if (player.getNationality() != null) {
    holder.nationalityLayout.setVisibility(View.VISIBLE); // <- SET VISIBLE, Well done!
    holder.nationality.setText(player.getNationality());

    holder.logoNationality.setVisibility(View.VISIBLE); // <- Missing line: SET VISIBLE as before.
    if (player.getNatImg() != null) {
       holder.logoNationality.setImageDrawable(flags.get(player.getNatImg()));
    } else {
        holder.logoNationality.setVisibility(View.INVISIBLE);  // <- OK, no logo, so hide it.
    }
} else {
    holder.nationalityLayout.setVisibility(View.GONE); // No nationality, hides entire layout.
}