GetView 对比。自定义 CursorAdapter 中的 BindView?

发布于 2024-09-15 12:33:46 字数 308 浏览 5 评论 0原文

所以,我正在观看此视频 http://www.youtube.com/watch?v=N6YdwzAvwOA 和 Romain Guy 展示了如何使用 getView() 方法编写更高效的 UI 适配器代码。这也适用于 CursorAdapters 吗?我目前正在使用 bindView()newView() 作为我的自定义光标适配器。我应该使用 getView 吗?

So, I'm watching this video http://www.youtube.com/watch?v=N6YdwzAvwOA and 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十二 2024-09-22 12:33:46

CursorAdapter 有一个 getView() 的实现,它委托给 newView()bindView(),在这样的情况下方式 as 强制执行行回收模式。因此,如果您要重写 newView()bindView(),则无需使用 CursorAdapter 进行任何特殊操作即可进行行回收。

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().

2024-09-22 12:33:46
/**
     * @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;
    }

这个CursorAdapter源码,显然cursorAdapter的工作量比较多。

/**
     * @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.

无力看清 2024-09-22 12:33:46

CursorAdapter 实现与子类化常规适配器(如 BaseAdapter)不同,您不需要重写 getView()getCount ()getItemId() 因为可以从游标本身检索该信息。

给定一个 Cursor,您只需要重写两个方法即可创建一个 CursorAdapter 子类:

bindView() :给定一个视图,更新它以显示提供的游标中的数据。

newView() :调用它来构造一个进入列表的新视图。

CursorAdapter 将负责回收视图(与常规 Adapter 上的 getView() 方法不同)。它不会在每次需要新行时调用 newView()。如果它已经有一个View(不是null),它会直接调用bindView(),这样,创建的视图就会被重用。通过将每个视图的创建和填充分为这两个方法,CursorAdapter 实现了视图重用,而在常规适配器中,这两件事都是在 getView() 方法中完成的。

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.

Given a Cursor, you only need to override two methods to create a CursorAdapter subclass:

bindView() : Given a view, update it to display the data in the provided cursor.

newView() : This gets called to consctruct a new view that goes into the the list.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文