GetView比.自定义CursorAdapter中的BindView?

Chr*_*rry 53 user-interface android adapter

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

Com*_*are 73

CursorAdapter有一个getView()代表的实现,newView()bindView()以这种方式强制执行行回收模式.因此,CursorAdapter如果您要覆盖newView()和排除,则不需要对行进行任何特殊操作bindView().

  • @Scienceprodigy:在`newView()`中,你将为该行创建`ViewHolder`并将其与`setTag()`相关联.在`bindView()`中,你将通过`getTag()`检索`ViewHolder`. (18认同)
  • 我将如何应用ViewHolder模式?我会在newView()和bindView()之间拆分吗? (6认同)
  • @Scienceprodigy:如果你有2种以上的行,你需要覆盖`getViewTypeCount()`和`getItemViewType()`. (3认同)
  • @ Christian13467:我真的建议你打开一个新问题,而不是评论一个18个月大的答案.话虽如此,如果你说你正在改变*一个位置的视图类型,那么在不强制完全重新加载`AdapterView`的情况下,这不太可能正常工作. (2认同)

Cro*_*ong 19

/**
     * @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;
    }
Run Code Online (Sandbox Code Playgroud)

这个CursorAdapter源代码,显然是cursorAdapter的工作原理.