LayoutTransition DISAPPEARING动画不适用于游标适配器

时间:2014-12-03 22:40:14

标签: android animation transition android-cursoradapter layouttransition

所以,标题几乎说明了一切。我有一个listview,其中填充了一个显示数据库数据的自定义游标适配器。我有一个动画,用于将项目添加到列表,工作正常,但我无法获取动画从列表中删除项目工作。我担心该项目正在从数据库中删除,并且在动画有机会执行之前正在刷新列表。感谢您解决此问题的任何帮助。

动画代码

    LayoutTransition transition = new LayoutTransition();
    Animator appearAnim = ObjectAnimator.ofFloat(null, "rotationX", 90f, 0f)
            .setDuration(android.R.integer.config_shortAnimTime);
    Animator disappearAnim = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f)
            .setDuration(android.R.integer.config_longAnimTime);
    transition.setAnimator(LayoutTransition.APPEARING, appearAnim);
    transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnim);
    mNotesListView.setLayoutTransition(transition);

删除备注方法

 @Override
    public void deleteNote(final String noteId) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                mNotesTable.deleteNote(mDb, noteId);
                int notebookNumber = mNoteBookFragment.getNotebookNumber();
                final Cursor cursor = mNotesTable.notesQuery(mDb, notebookNumber);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mNoteBookFragment.refreshNoteList(cursor);
                        Toast.makeText(BlocNotes.this,
                                getString(R.string.delete_note_toast), Toast.LENGTH_LONG).show();
                    }
                });
            }
    }.start();
}

并刷新列表

public void refreshNoteList(Cursor cursor) {
        mNoteAdapter.changeCursor(cursor);
        mNoteAdapter.notifyDataSetChanged();

        setNewNoteText(""); //clear the text

    } 

已修改的刷新方法

 public void refreshNoteList(Cursor cursor) {
        mTransition.removeChild(mNoteAdapter.getParent(), mNoteAdapter.getView());
        mNoteAdapter.changeCursor(cursor);
        mNoteAdapter.notifyDataSetChanged();

        setNewNoteText(""); //clear the text

    }

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。在刷新removeChild(parentView, childView)之前,您应该在Transition上致电ListView。这可能不是最好的方法,但它有效。

顺便说一下,我在这里使用Loaderhttp://developer.android.com/guide/components/loaders.html

UPD:动画结束后你需要运行其余的代码(否则会被中断)。像这样:

public void refreshNoteList(Cursor cursor) {
    transition.removeChild(ListView mNotesListView, View mNotesListView.getChildAt(int position);
    new Handler().postDelayed(new Runnable(){
        public void run() {
            mNoteAdapter.changeCursor(cursor);
            mNoteAdapter.notifyDataSetChanged();

            setNewNoteText(""); //clear the text
        }
    }, disappearAnim.getDuration());
}