【发布时间】:2011-03-15 21:48:47
【问题描述】:
我想知道如何管理 ListView 中的视图。
我有一个在 ListView 上设置的自定义 Adapter,这个 Adapter 覆盖了 getView 方法
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
v = mInflater_.inflate(R.layout.news_newsentry, null);
}
final NewsItem newsItem = getItem(position);
if (newsItem != null) {
// Do stuff
}
return v;
}
但问题是,当用户点击一个项目时,我会稍微改变视图以使其更大。它工作得很好,但是当项目视图被回收时,它会保持“大”高度来显示另一个项目。
为了防止这种情况,我每次都更改了代码以创建一个新视图
变化:
View v = convertView;
if (v == null) {
v = mInflater_.inflate(R.layout.news_newsentry, null);
}
由
View v = mInflater_.inflate(R.layout.news_newsentry, null);
现在的问题是,当项目从列表中消失并重新出现(列表滚动)时,视图是全新的并且高度设置为“小”。
然后我的问题是:如何管理项目视图以保留其属性,而不会弄乱其他视图和视图回收?
【问题讨论】: