【问题标题】:Why notifyItemRemoved works slowly?为什么 notifyItemRemoved 工作缓慢?
【发布时间】:2015-08-13 18:02:13
【问题描述】:

我在 RecyclerView 中实现了滑动和删除功能。但问题是,当我删除项目时,前一个项目上升,在这个项目下面我看到同一个项目几秒钟,但在删除只有一个项目可见(上一个)之后我也尝试使用 adapter.notifyDataSetChanged() 但是当我刷过该项目时,我可以在之前的位置看到该项目几秒钟,不到 1 秒后项目正在删除。

所以,例如我有列表

  1. 列表项1
  2. 列表项2

滑动并调用 notifyItemRemoved() 后,我看到这样的列表 1 秒

  1. 列表项2
  2. 列表项2

最后

  1. 列表项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


    【解决方案1】:

    解决方案是在删除项目后调用 swapCursor()。在这种情况下,一切正常。

     @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);
                adapter_.swapCursor(ChatsTableHelper.getAllChatsInfo(ChatListActivity.this));
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2011-10-26
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 2013-10-01
      • 2018-11-21
      • 1970-01-01
      相关资源
      最近更新 更多