0

我完全赞成在列表视图中重用视图。我总是在getView中再次设置所有控件的可见性、内容、宽度等。不幸的是,ListView 似乎无法重新计算高度。

图一显示了最初显示的项目: 在此处输入图像描述

图二显示了在我们滚动并返回后第一个项目是如何呈现的 在此处输入图像描述

背景线性布局高度(黑色区域)让我觉得在图二中,Android 正在重用一个视图,该视图只显示了一个更高的项目(例如第二个项目)。但是,为什么它不重新校准/重置/重新计算自身(它在其 XML 中处于“wrap_content”模式)当作为第一个项目的视图重用时,内容(文本 + 图像)不那么高?

事实上,我不确定发生了什么。只有当我在视图中有图像时,问题才会显现出来。我尝试以不同的方式(下面的示例代码)组织位图/图像加载,并注释掉不同的内容,但这似乎没有太大区别。对于原因,我真的很茫然。

override_listitem_news.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
 android:background="@android:color/black"        
        >                           
        <TextView
                android:id="@+id/listitem_news_label"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textSize="22sp"
                android:padding="5dip"
                android:text="@string/newsItemTitle"/>           
        <TextView
                android:id="@+id/listitem_news_date"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textStyle="italic"
                android:textSize="15sp"
                android:padding="5dip"
                android:text="@string/newsItemDate"/>                   
        <TextView
                android:id="@+id/listitem_news_content"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textStyle="normal"
                android:textSize="15sp"
                android:padding="5dip"
                android:autoLink="web"
                android:text="@string/newsItemDesc"                    
android:background="@android:color/darker_gray"                
                />                    
<ImageView
        android:id="@+id/listitem_news_icon"
        android:layout_width="match_parent"                                   
        android:layout_height="wrap_content"                        
    />        
</LinearLayout>

这是我在getView中加载图像的代码

                        ViewTreeObserver vto = image.getViewTreeObserver();
                        vto.addOnGlobalLayoutListener(
                          new OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                              image.getViewTreeObserver().removeGlobalOnLayoutListener(this);                                                                 
                              image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                              SharedCode.sharedUtilScaleImage_Width(image);                                      
                            }
                          }                        
                        );                                                                   

                        image.setTag(data.image_file_name + data.image_file_url);
                        Bitmap bit = null;
                        bit = SharedCode.sharedGetFileFromOffline(thisActivityContext, "news", data.image_file_name, MyGetKindOfFile.ImageAsBitmap).bitmap;

                        if (bit != null) {                                                                       
                          image.setImageBitmap(bit);                                                                                                                                                      
                          image.setVisibility(View.VISIBLE);                                  
                        }
                        else {
                          image.setImageBitmap(null);                                  
                          image.setVisibility(View.GONE);
                        }
                       image.setPadding(0, 0, 0, 0);                                                                                                               
                       image.setBackgroundColor(data.backgroundColorInt);                                
4

1 回答 1

0

For what it is worth, problem appeared to be related to the imageview. Just for reference, I will write here how I solved it.

  1. In getView I fixed the imageview width to screen width (instead of "wrap-content" and/or parent view width - earlier code used OnGlobalLayoutListener for parent width)
  2. I switched over to using SetDrawable instead of SetImageBitmap. It is odd, but this difference was actual very important in solving the odd space around the imageview after scrolling an item/row in/out of view.

My research did also indicate that others had problems using wrap_content in listview for cases similar to mine, but I was not able to find anyone who had experienced exact same problems as me.

于 2013-07-14T22:06:13.223 回答