[英]Layout messed up in ListView after scrolling
我有一個帶有自定義適配器的ListView,該適配器創建的行包含可能包含1或2行文本的TextView。
news_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="48dip"
android:padding="6dp"
android:textSize="14sp">
</TextView>
</LinearLayout>
適配器:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.news_row, null);
}
else {
TextView titleView = (TextView)convertView.findViewById(R.id.text);
System.out.println(titleView.getText() + " to **" +
stories.get(position).title);
}
TextView titleView = (TextView)convertView.findViewById(R.id.text);
titleView.setText(stories.get(position).title);
titleView.setGravity(Gravity.CENTER_VERTICAL);
return convertView;
}
滾動后,某些單行TextViews會與行的頂部對齊,而某些2行TextViews則在行的底部。 當最初包含兩行TextView的行被賦予一行文本時,似乎會發生問題,反之亦然。
我以為在getView中設置Gravity可以解決此行回收問題,但這必須是其他問題。 如果每次回收行時都以編程方式創建TextView怎么辦? 這就是“重置”它所需要的嗎?
行視圖在listView中重新使用。 第一次充氣十個(我猜是十個或更少),並設置大小/重力等。 當用戶滾動到可見行的下方時,頂部行將被回收並饋送到下一行。 因此,最上面一行的視圖可用於下一個即將到來的行。 如果您的第一行和下一行的行數不同,則預期會出現這種行為。
解。 設置文本重力和初始充氣時的行數,無論行數多少,所有行的大小均相同。 您應該不再看到觀察到的波動。
有關更多詳細信息,請參見以下說明:http://android.amberfog.com/?p = 296
解決方案是設置textView的行數(如Veeresh所說),但不要在初始充氣時設置。
titleView.setLines(2);
不完全令人滿意(如果我有n
行,該怎么辦?),考慮到在布局文件中已經明確設置了textView的高度,這甚至沒有任何意義。 那好吧。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.