CursorAdapter 支持 ListView 删除动画“闪烁"删除时 [英] CursorAdapter backed ListView delete animation "flickers" on delete

查看:14
本文介绍了CursorAdapter 支持 ListView 删除动画“闪烁"删除时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SwipeToDismissUndoList 库在 ListView 中实现滑动删除扩展 Roman Nurik 的 SwipeToDismiss 示例.

I'm trying to implement swipe to delete and in a ListView using the SwipeToDismissUndoList library which extends Roman Nurik's SwipeToDismiss sample.

我的问题在于删除动画.由于 ListViewCursorAdapter 支持,动画会触发 onAnimationEnd 中的 onDismiss 回调,但这意味着在 CursorAdapter 随着删除更新之前,动画已运行并自行重置.

My issue is in the delete animation. Since the ListView is backed by a CursorAdapter, the animation triggers the onDismiss callback in onAnimationEnd but this means that the animation has run and reset itself before the CursorAdapter updates with the delete.

对于用户来说,这最终看起来像是一个闪烁,他们通过将笔记滑开来删除笔记,然后视图返回一瞬间然后消失,因为 CursorAdapter 已经拾取了数据改变.

This ends up looking like a flicker to the user where they delete a note by swiping it away, then the view is back for a split second and then disappears because the CursorAdapter has picked up the data change.

这是我的OnDismissCallback:

private SwipeDismissList.OnDismissCallback dismissCallback = 
        new SwipeDismissList.OnDismissCallback() {
    @Override
    public SwipeDismissList.Undoable onDismiss(ListView listView, final int position) {
        Cursor c = mAdapter.getCursor();
        c.moveToPosition(position);
        final int id = c.getInt(Query._ID);
        final Item item = Item.findById(getActivity(), id);
        if (Log.LOGV) Log.v("Deleting item: " + item);

        final ContentResolver cr = getActivity().getContentResolver();
        cr.delete(Items.buildItemUri(id), null, null);
        mAdapter.notifyDataSetChanged();

        return new SwipeDismissList.Undoable() {
            public void undo() {
                if (Log.LOGV) Log.v("Restoring Item: " + item);
                ContentValues cv = new ContentValues();
                cv.put(Items._ID, item.getId());
                cv.put(Items.ITEM_CONTENT, item.getContent());
                cr.insert(Items.CONTENT_URI, cv);
            }
        };
    }
};

推荐答案

我知道这个问题已被标记为已回答",但正如我在评论中指出的那样,使用 MatrixCursor 的问题在于效率太低.复制除要删除的行之外的所有行意味着行删除以线性时间运行(与列表视图中的项目数成线性关系).对于大数据和较慢的手机,这可能是不可接受的.

I know this question has been tagged as "answered" but as I pointed out in the comments, the problem with using a MatrixCursor is that it's too inefficient. Copying all the rows except for the row to be deleted means that row deletion runs in linear time (linear in the number of items in the listview). For large data and slower phones, that's probably unacceptable.

另一种方法是实现您自己的 AbstractCursor,忽略要删除的行.这会导致假行删除在恒定时间内运行,并且在绘制时性能损失可以忽略不计.

An alternate approach is to implement your own AbstractCursor that ignores the row to be deleted. This results in the fake row deletion running in constant time and with negligible performance penalty when drawing.

示例实现:

public class CursorWithDelete extends AbstractCursor {

private Cursor cursor;
private int posToIgnore;

public CursorWithDelete(Cursor cursor, int posToRemove)
{
    this.cursor = cursor;
    this.posToIgnore = posToRemove;
}

@Override
public boolean onMove(int oldPosition, int newPosition)
{
    if (newPosition < posToIgnore)
    {
        cursor.moveToPosition(newPosition);
    }
    else
    {
        cursor.moveToPosition(newPosition+1);
    }
    return true;
}

@Override
public int getCount()
{
    return cursor.getCount() - 1;
}

@Override
public String[] getColumnNames()
{
    return cursor.getColumnNames();
}

//etc.
//make sure to override all methods in AbstractCursor appropriately

按照之前的所有步骤进行操作,除了:

Follow all the steps as before except:

  • 在 SwipeDismissList.OnDismissCallback.onDismiss() 中,创建新的 CursorWithDelete.
  • 交换新光标

这篇关于CursorAdapter 支持 ListView 删除动画“闪烁"删除时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆