【发布时间】:2015-08-13 18:02:13
【问题描述】:
我在 RecyclerView 中实现了滑动和删除功能。但问题是,当我删除项目时,前一个项目上升,在这个项目下面我看到同一个项目几秒钟,但在删除只有一个项目可见(上一个)之后我也尝试使用 adapter.notifyDataSetChanged() 但是当我刷过该项目时,我可以在之前的位置看到该项目几秒钟,不到 1 秒后项目正在删除。
所以,例如我有列表
- 列表项1
- 列表项2
滑动并调用 notifyItemRemoved() 后,我看到这样的列表 1 秒
- 列表项2
- 列表项2
最后
- 列表项2
这里是代码
ItemTouchHelper swipeToDismissTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
// callback for drag-n-drop, false to skip this feature
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// callback for swipe to dismiss, removing item from data and adapter
int itemPosition = viewHolder.getAdapterPosition();
Log.v("Position", Integer.toString(itemPosition));
adapter_.removeItem(itemPosition);
}
});
swipeToDismissTouchHelper.attachToRecyclerView(chatsRecyclerView_);
我的适配器中的here方法
public void removeItem(int position){
Cursor cursor = getCursor();
if (cursor.getCount() != 0) {
Log.v("Size", Integer.toString(cursor.getCount()));
cursor.moveToPosition(position);
String chatIdOnServer = cursor.getString(cursor.getColumnIndex(MegaFleetDatabaseOpenHelper.ChatsTable.ID_ON_SERVER));
context_.startService(ChatsInfoUpdateService.createLeaveChatIntent(this.context_, chatIdOnServer));
ChatsTableHelper.deleteChat(this.context_, chatIdOnServer);
// this.notifyDataSetChanged();
this.notifyItemRemoved(position);
}
}
【问题讨论】:
标签: android android-recyclerview swipe-gesture