Android ListView中最后一项的ImageView的奇怪行为
我有一个带有自定义适配器的ListView。每行中都有一个仅在特定约束下可见的ImageView。问题是,如果第一行的ImageView可见,那么最后一行也是可见的,反之亦然 这是我的适配器的Android ListView中最后一项的ImageView的奇怪行为,android,android-listview,android-imageview,Android,Android Listview,Android Imageview,我有一个带有自定义适配器的ListView。每行中都有一个仅在特定约束下可见的ImageView。问题是,如果第一行的ImageView可见,那么最后一行也是可见的,反之亦然 这是我的适配器的getView()代码 public View getView(int position, View view, ViewGroup parent) { if (view == null) { LayoutInflater inflater = (LayoutInflater) mC
getView()
代码
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_idea, null);
}
Idea idea = mIdeas.get(position);
if (idea != null) {
ImageView imgAlarm = (ImageView) view
.findViewById(R.id.imgAlarm_rowIdea);
if (idea.getTimeReminder() != null)
imgAlarm.setVisibility(ImageView.VISIBLE);
TextView lblTitle = (TextView) view
.findViewById(R.id.lblTitle_rowIdea);
lblTitle.setText(idea.getTitle());
TextView lblDescription = (TextView) view
.findViewById(R.id.lblDescription_rowIdea);
lblDescription.setText(idea.getDescription());
}
return view;
}
mIdeas
是一个ArrayList
,其中包含要在列表视图中显示的所有数据。
imgAlarm
是我上面告诉过的ImageView
。如果不满足条件,您需要恢复ImageView
的可见性状态,这样您就不会对潜在的回收视图产生问题(可能会使ImageView
已经可见,并且在不应该的时候出现):
如果不满足该条件,您需要恢复ImageView
的可见性状态,这样您就不会对潜在的回收视图产生问题(该视图可能会使ImageView
已经可见,并且在不应该的时候出现):
改变
到
这里发生的事情是适配器正在“回收”视图。因此,如果您在测试中看到,最后一个视图和第一个视图实际上是同一个实例。更改
if (idea.getTimeReminder() != null)
imgAlarm.setVisibility(ImageView.VISIBLE);
到
这里发生的事情是适配器正在“回收”视图。因此,在测试中看到的情况下,最后一个视图和第一个视图实际上是同一个实例
if (idea.getTimeReminder() != null)
imgAlarm.setVisibility(ImageView.VISIBLE);
if (idea.getTimeReminder() != null)
imgAlarm.setVisibility(ImageView.VISIBLE);
else
imgAlarm.setVisibility(ImageView.GONE);