Android GetView 对比 自定义 CursorAdapter 中的 BindView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3535074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GetView Vs. BindView in a custom CursorAdapter?
提问by Christopher Perry
So, I'm watching this video http://www.youtube.com/watch?v=N6YdwzAvwOAand Romain Guy is showing how to make more efficient UI adapter code using the getView()
method. Does this apply to CursorAdapters as well? I'm currently using bindView()
and newView()
for my custom cursor adapters. Should I be using getView instead?
所以,我正在观看这个视频http://www.youtube.com/watch?v=N6YdwzAvwOA并且 Romain Guy 正在展示如何使用该getView()
方法制作更高效的 UI 适配器代码。这也适用于 CursorAdapters 吗?我目前正在使用bindView()
和newView()
用于我的自定义光标适配器。我应该使用 getView 吗?
回答by CommonsWare
CursorAdapter
has an implementation of getView()
that delegates to newView()
and bindView()
, in such a way as enforces the row recycling pattern. Hence, you do not need to do anything special with a CursorAdapter
for row recycling if you are overriding newView()
and bindView()
.
CursorAdapter
具有getView()
委托给newView()
和的实现,bindView()
以强制行回收模式的方式。因此,CursorAdapter
如果您覆盖newView()
and ,则不需要对for 行回收做任何特别的事情bindView()
。
回答by Crossle Song
/**
* @see android.widget.ListAdapter#getView(int, View, ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
View v;
if (convertView == null) {
v = newView(mContext, mCursor, parent);
} else {
v = convertView;
}
bindView(v, mContext, mCursor);
return v;
}
This CursorAdapter source code, clearly cursorAdapter work more.
这个 CursorAdapter 源代码,显然 cursorAdapter 工作更多。
回答by Yogesh Umesh Vaity
The CursorAdapter
implementation is different from sub-classing regular adapters like BaseAdapter
, you don't need to override getView()
, getCount()
, getItemId()
because that information can be retrieved from the cursor itself.
该CursorAdapter
实现不同于子类化常规适配器,例如BaseAdapter
,您不需要覆盖getView()
, getCount()
,getItemId()
因为可以从游标本身检索该信息。
Given a Cursor
, you only need to override two methods to create a CursorAdapter
subclass:
给定 a Cursor
,您只需要覆盖两个方法即可创建CursorAdapter
子类:
bindView()
: Given a view, update it to display the data in the provided cursor.
bindView()
:给定一个视图,更新它以在提供的光标中显示数据。
newView()
: This gets called to consctruct a new view that goes into the the list.
newView()
:这被调用来构造一个进入列表的新视图。
The CursorAdapter
will take care of recycling views (unlike the getView()
method on regular Adapter
). It doesn't call the newView()
each time it needs a new row. If it already has a View
(not null
), it will directly call the bindView()
, this way, the created view is reused. By splitting the creation and population of each view into these two methods, the CursorAdapter
achieves view reuse where as, in regular adapters, both these things are done in getView()
method.
该CursorAdapter
会照顾回收的意见(不像的getView()
定期方法Adapter
)。它不会在newView()
每次需要新行时调用。如果它已经有一个View
(not null
),它会直接调用bindView()
,这样,创建的视图被重用。通过将每个视图的创建和填充拆分为这两种方法,CursorAdapter
实现了视图重用,而在常规适配器中,这两种事情都是在getView()
方法中完成的。