Android - 带 ArrayAdapter 和隐藏 TextView 的 ListView
Android - ListView with ArrayAdapter and hidden TextViews
我的 Android 应用程序的 ListView 中的 ArrayAdapter 有问题。应该显示的 TextViews 有两种可能性,带有信息文本或没有信息文本。滚动时出现问题。当我再次向下和向上滚动时,没有信息文本的 TextView 现在有一个随机信息文本(来自带有信息文本的 TextView 之一)。问题的出现是因为 ListView 的内存使用。现在我正在寻找解决问题的可能性。在没有信息文本的情况下滚动后,结果应该显示 TextViews(没有信息文本)。那么在 ListView 中使用 ArrayAdapter 是否有任何好的解决方案,或者是否有任何其他 Android 小部件可供使用?
Java代码(Activityclass):
@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
。视图正在被回收,由于之前已经展示过,传回给你的时候还是可见的。
通常在绑定时您需要绑定所有状态,包括可见性更改。由于视图的重用,不要假设 inflation 的任何状态。
试试这个:
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);
}
我的 Android 应用程序的 ListView 中的 ArrayAdapter 有问题。应该显示的 TextViews 有两种可能性,带有信息文本或没有信息文本。滚动时出现问题。当我再次向下和向上滚动时,没有信息文本的 TextView 现在有一个随机信息文本(来自带有信息文本的 TextView 之一)。问题的出现是因为 ListView 的内存使用。现在我正在寻找解决问题的可能性。在没有信息文本的情况下滚动后,结果应该显示 TextViews(没有信息文本)。那么在 ListView 中使用 ArrayAdapter 是否有任何好的解决方案,或者是否有任何其他 Android 小部件可供使用?
Java代码(Activityclass):
@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
。视图正在被回收,由于之前已经展示过,传回给你的时候还是可见的。
通常在绑定时您需要绑定所有状态,包括可见性更改。由于视图的重用,不要假设 inflation 的任何状态。
试试这个:
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);
}