我之所以这样问是因为一切都很好,但我不明白为什么会这样实现。
我已经阅读了如何在这里使用convertView:
What is the purpose of `convertView` in ListView adapter?
但后来我读了这段代码:(链接:https://github.com/paraches/ListViewCellDeleteAnimation)
ViewHolder
- 查看标记信息对象在deleteCell
函数中,我们将(needInflate
)的ViewHolder
布尔值设置为true,因此我们提供了可以重用此视图的信息。当我们创建新标记时,我们将view
标记设置为false ...
为什么在第二个if
语句中,假设needInflate
为真(view
可以重复使用),在花括号中,我们处于新的view
?那里不应该反过来,第二个是第三个(view = convertView;
),反之亦然? getView
功能:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
...
if (convertView==null) {
view = mInflater.inflate(R.layout.chain_cell, parent, false);
setViewHolder(view);
}
else if (((ViewHolder)convertView.getTag()).needInflate) {
view = mInflater.inflate(R.layout.chain_cell, parent, false);
setViewHolder(view);
}
else {
view = convertView;
}
vh = (ViewHolder) view.getTag();
vh.text.setText(cell.name);
vh.imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
deleteCell(view, position);
}
});
return view;
}
编辑当我做上面将要解释的内容更改第二和第三个声明时,会创建一些填充或略微余地我不知道......
答案 0 :(得分:0)
ListViews can be large. Furthermore, usually a listview's item only changes its content from one another. ie: Each entry in a listview usually has the same UI elements. Consider the following image as a reference.
As you can see, each cell has
From one cell to another, only the values of the cell changes. Even more, at once we don't need to display all the cells. We only need to display whatever should be visible on the screen at the given time.
Creating a view does take some time. When you add have a big list of items (Say 100 items) and you try to create all of them at once, you'll start to notice unresponsiveness of the app.
Considering all these factors, we use a method called "Reusing" views. What we simply do is just as the name suggests. We simply reuse existing view objects. We change only the display values and do not create new views if we can reuse an existing view.
As you know, an adapter's getView method gets called when the listview is drawn. As you should know, a listView's getView method gets called whenever you scroll down the list in addition to the creation time.
So, suppose you scroll down the view. As you scroll down, the first cell (Berlin) disappears from the view. Now, it becomes ready to be reused.
So, whenever you get such a reusable cell, the Android OS would call the getView method and pass that reusable view as the parameter "convertView". So, if you basically get a non-null "convertView", that implies you don't need to inflate a view and you can use and existing view.