【问题标题】:Prevent re-use of some views in ListView (custom cursor-adapter)防止重复使用 ListView 中的某些视图(自定义光标适配器)
【发布时间】:2012-12-30 14:15:13
【问题描述】:

是否可以将在newView(..) 期间创建的一个 视图添加为单独的类型,以便在bindView(...) 期间防止重复使用该视图?

这是我的自定义 cursorAdapter 的样子:

@Override
public void bindView(View vi, Context arg1, Cursor cursor) {
    priority.setText(cursor.getString(cursor.getColumnIndex(TodoTable.COLUMN_PRIORITY)));TextView timeElapsed =  (TextView)vi.findViewById(R.id.todayTime); //time
    long id = cursor.getLong(cursor.getColumnIndex(TodoTable.COLUMN_ID));

    if(ts.getRunning() == id){
        ts.startTimer(vi, ts.getCurrentTaskStart(), id);
    }else{
        long time = cursor.getLong((cursor.getColumnIndex(TodoTable.COLUMN_TIME)));
        timeElapsed.setText(ts.getTimeString(0, 0, time));
    }
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup arg2) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View vi = inflater.inflate(R.layout.list_item, null);
    bindView(vi, context, cursor);
    return vi;
}

我尝试使用 getItemViewTypegetViewTypeCount 将其添加到不同的类型,但这只能处理位置,而不是与列表视图的行关联的 ID。

我想防止在创建时使用ts.getRunning() == id 的视图在我滚动时在其他位置重复使用。我该怎么做这样的事情?

【问题讨论】:

    标签: android listview android-cursoradapter


    【解决方案1】:

    如果您想防止在某个位置重复使用视图,请执行以下操作

    @Override
    public int getItemViewType(int position) {
        mCursor.moveToPosition(position);
        if (mCursor.getLong(mCursor.getColumnIndex(TodoTable.COLUMN_ID)) == ts.getRunning()){
            return IGNORE_ITEM_VIEW_TYPE;
        }
        return super.getItemViewType(position);
    }
    

    如果您在getItemViewType(position) 中返回IGNORE_ITEM_VIEW_TYPE,则该位置的视图将不会被回收。有关IGNORE_ITEM_VIEW_TYPE 的更多信息是here

    【讨论】:

    • 我不知道相对于列表视图的位置。仅与游标中的 ID 字段相关联。有没有办法从另一个得到一个?
    • 你不需要知道位置。当您滚动列表并且在光标的该位置时,列 COLUMN_ID 的值等于 ts.getRunning() 时,该位置的视图将不会被回收
    • @SreejithKrishnanR 你好,它是如何工作的? IGNORE_ITEM_VIEW_TYPE 应该签入 newView 或 bindView 吗?或自动显示视图??请帮助我谢谢:((
    【解决方案2】:

    重写 getView 方法。如果您查看 CursorAdapter (link) 的源代码,您会看到他们检查视图是否为空的地方。您只需复制整个方法,并添加一个额外的 if 检查 ts.getRunning() == id

    这是它现在的样子 -

    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 || ts.getRunning() == id) {
            v = newView(mContext, mCursor, parent);
        } else {
            v = convertView;
        }
        bindView(v, mContext, mCursor);
        return v;
    }
    

    谢天谢地,该方法中使用的所有字段似乎都受到保护,因此您应该不会遇到任何问题。

    【讨论】:

    • 如何在getView() 中访问id? id 来自游标本身。
    • 来自字段 mCursor。传递给 bindview 的光标是字段本身。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多