0

私はリストビューでビューを再利用することに賛成です。getView ですべてのコントロールの可視性、コンテンツ、幅などを常に再設定します残念ながら ListView は高さの再計算に失敗しているようです。

写真 1 は、最初に表示されたアイテムを示しています。 ここに画像の説明を入力

画像 2 は、アイテム 1 がスクロールして戻ってきた後にどのようにレンダリングされるかを示しています。 ここに画像の説明を入力

背景の linearlayout の高さ (黒い領域)見ると、図 2 では、Android がより高いアイテム (2 番目のアイテムなど) を表示していたビューを再利用していると思いました。しかし、コンテンツ (テキスト + 画像) がそれほど高くない最初のアイテムのビューとして再利用されると、なぜそれ自体が再調整/リセット/再計算されないのですか (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

それだけの価値があるため、問題はイメージビューに関連しているように見えました。参考までに、私がどのように解決したかをここに書きます。

  1. getViewで、イメージビューの幅を画面の幅に固定しました (「wrap-content」や親ビューの幅の代わりに - 以前のコードでは親の幅に OnGlobalLayoutListener を使用していました)
  2. SetImageBitmap の代わりに SetDrawable を使用するように切り替えました。奇妙ですが、この違いは、アイテム/行をスクロールして表示/非表示にした後、イメージビューの周りの奇妙なスペースを解決する上で実際に非常に重要でした.

私の調査では、リストビューで wrap_content を使用して私の場合と同様の問題を抱えている人もいることが示されましたが、私とまったく同じ問題を経験した人を見つけることができませんでした。

于 2013-07-14T22:06:13.223 に答える