所以,标题几乎说明了一切。我有一个列表视图,其中填充了一个自定义游标适配器,该适配器显示数据库中的数据。我有一个用于将项目添加到列表的动画,它工作正常,但无法使从列表中删除项目的动画正常工作。我担心该项目正在从数据库中删除,并且在动画有机会执行之前刷新列表。我感谢解决此问题的任何帮助。
动画代码
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
}
我遇到了同样的问题。在刷新ListView
之前,您应该在Transition
上调用removeChild(parentView, childView)
。这可能不是最好的方法,但它有效。
顺便说一下,我会在这里使用一个Loader
:http://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());
}