[英]Android - ListView with ArrayAdapter and hidden TextViews
我的Android應用程序中的ListView中的ArrayAdapter出現問題。 應該顯示的TextView具有兩個可能,帶有信息文本或不帶信息文本。 滾動時出現問題。 當我再次上下滾動時,沒有信息文本的TextViews現在具有隨機的信息文本(來自帶有信息文本的TextViews之一)。 出現此問題是由於ListView的內存使用情況。 現在,我正在尋找解決問題的可能性。 滾動而不顯示信息文本后,結果應顯示TextViews(不顯示信息文本)。 那么在ListView中使用ArrayAdapter是否有任何好的解決方案,或者還有其他可使用的Android小部件嗎?
Java代碼(活動類):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(layout, null);
holder.text = (TextView) view.findViewById(R.id.list_layout_textview);
holder.infoText = (TextView) view.findViewById(R.id.list_layout_infos);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
final OutputTweet outputTweet = tweetObjects.get(position);
if (outputTweet.getText() != null) {
holder.text.setText(outputTweet.getText());
}
if (!outputTweet.getInfoText().equals("")) {
holder.infoText.setVisibility(View.VISIBLE);
holder.infoText.setText(outputTweet.getInfoText());
}
return view;
}
}
布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_layout"
>
<TextView
android:id="@+id/list_layout_infos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_info_color"
android:layout_marginTop="2dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:textSize="12sp"
android:textStyle="italic"
android:visibility="gone"
/>
<TextView
android:id="@+id/list_layout_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/textColor"
android:layout_marginTop="2dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:fontFamily="sans-serif-light"
android:typeface="sans"
android:textSize="15sp"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
謝謝您的解決方案:)
您需要在最后一個if子句中添加else子句,以將視圖設置回View.GONE
或View.INVISIBLE
。 該視圖已被回收,並且由於之前已顯示,因此將其傳遞回給您時仍將可見。
通常,綁定時需要綁定ALL狀態,包括可見性更改。 不要因為重用視圖而從通貨膨脹中假設任何狀態。
嘗試這個:
if (outputTweet.getText() != null) {
holder.text.setText(outputTweet.getText());
} else {
holder.text.setText("");
}
if (!outputTweet.getInfoText().equals("")) {
holder.infoText.setVisibility(View.VISIBLE);
holder.infoText.setText(outputTweet.getInfoText());
} else {
holder.infoText.setVisibility(View.GONE);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.