我在一个ListView项目中有一个TextView和一个ImageView。他们两个或只是其中一个可能是空的。
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:visibility="gone" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_marginTop="10dp"
android:visibility="gone" />
问题是当它们是空的时候它们不会消失并且它们充满了来自其他ListView项目的数据。
如何解决这个问题?
public View getView(int position, View view, ViewGroup parent){
View v = view;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.single_item, null);
}
String url = results.get(position).get("url");
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
if (!url.equalsIgnoreCase("")) {
if (imageView.getVisibility() == View.GONE) {
imageView.setVisibility(View.VISIBLE);
}
Picasso.with(getApplicationContext())
.load(url)
.into(imageView);
}
String text = results.get(position).get("text");
TextView textView = (TextView) v.findViewById(R.id.textView);
if (!text.equalsIgnoreCase("")) {
if (textView.getVisibility() == View.GONE) {
textView.setVisibility(View.VISIBLE);
}
textView.setText(text);
}
return v;
}
答案 0 :(得分:-2)
由于ListView会回收旧视图项,因此您需要在使用时清除每个视图:
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
if (!url.equalsIgnoreCase("")) {
if (imageView.getVisibility() == View.GONE) {
imageView.setVisibility(View.VISIBLE);
}
Picasso.with(getApplicationContext())
.load(url)
.into(imageView);
} else {
imageView.setImageDrawable(null);
}
TextView textView = (TextView) v.findViewById(R.id.textView);
if (!text.equalsIgnoreCase("")) {
if (textView.getVisibility() == View.GONE) {
textView.setVisibility(View.VISIBLE);
}
textView.setText(text);
} else {
textView.setText("");
}