私はリストビューでビューを再利用することに賛成です。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);