Prevent re-use of some views in ListView (custom cursor-adapter)

你。 提交于 2019-12-05 07:51:08

问题


Is it possible to add one view created during newView(..) as a separate type, so that reuse of just that view is prevented during bindView(...)?

This is what my custom cursorAdapter looks like:

@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;
}

I tried adding it to a different type, using getItemViewType and getViewTypeCount but that can only deals with positions and not the IDs associated with the rows of the listview.

I want to prevent the view with ts.getRunning() == id at the time of creation being reused at other positions when I scroll. How would I do something like this?


回答1:


If you want to prevent the reuse of view at a certain position the do the following

@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);
}

If you return IGNORE_ITEM_VIEW_TYPE in getItemViewType(position) then the view at that position will not be recycled. More info about IGNORE_ITEM_VIEW_TYPE is here




回答2:


Override the getView method. If you look at the source code of CursorAdapter (link), you'll see a place where they check if the view is null. You just need to copy the whole method, and add an additional if check for ts.getRunning() == id

Here is how it should now look like -

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;
}

Thankfully, it looks like all the fields used in that method are protected, so you should not run into any problems.



来源:https://stackoverflow.com/questions/14091423/prevent-re-use-of-some-views-in-listview-custom-cursor-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!