简体   繁体   English

从RecyclerView删除项目无法正常工作

[英]Removing item from RecyclerView not working well

I didn't customize anything on RecyclerView so there's default animation on item added/removed. 我没有在RecyclerView上自定义任何内容,因此添加/删除了项目的默认动画。 While I found the animation isn't working as I expected. 当我发现动画无法正常运行时。 When I removed item by the following code: 当我通过以下代码删除项目时:

mComments.remove(position);
notifyItemRemoved(position);

I see on UI it always remove the wrong one, and the one should be removed already keeps showing up and overlay the others. 我在UI上看到它总是删除错误的一个,而应该删除的一个已经一直显示并覆盖其他。

new CountDownTimer(60000, 1000) {
    @Override
    public void onTick(long l) {
        for (int i = 0; i < mComments.size(); i++) {
            RoomMessage item = mComments.get(i);
            item.timeRemaining -= 1000;

            if (item.timeRemaining <= 0) {
                Log.v(TAG, "Going to remove no." + i + ". And the content = " + mComments.get(i).text);
                removeAt(i);
            }
        }
    }

    @Override
    public void onFinish() {
        start();
    }
}.start();

According to log, I did remove the right one. 根据日志,我确实删除了正确的一个。 See as below. 见下文。

02-16 15:26:38.274 21861-21861/com.xxxx.android V/ChatsAdapter: Going to remove no.0. And the content = 1
02-16 15:26:41.284 21861-21861/com.xxxx.android V/ChatsAdapter: Going to remove no.0. And the content = 2
02-16 15:26:42.284 21861-21861/com.xxxx.android V/ChatsAdapter: Going to remove no.0. And the content = 3

What did I do wrong? 我做错了什么?

btw I've set setisRecyclable(false) 顺便说一句我已经设置了setisRecyclable(false)

Use only notifyItemRemoved(position); 仅使用notifyItemRemoved(position); .You are using both. 您同时使用两者。 notifyItemRemoved which is for notifying that items previously located at position has been removed from the data set. notifyItemRemoved ,用于通知先前位于该位置的项目已从数据集中删除。 While notifyItemRangeChanged(int positionStart, int itemCount) is used only when notify any registered observers that the itemCount items starting at position positionStart have changed. 虽然notifyItemRangeChanged(int positionStart, int itemCount)仅在通知任何注册的观察者从positionpositionStart开始的itemCount项目已更改时才使用。 If you are adding single item use notifyItemInserted . 如果要添加单个项目,请使用notifyItemInserted If you have added more then one new items then use notifyItemRangeInserted(int positionStart, int itemCount) . 如果添加了更多项,则使用notifyItemRangeInserted(int positionStart, int itemCount) Any method ending with Changed states that the value or values of that particular item or row has changed. 任何以Changed结尾的方法都表明该特定项目或行的一个或多个值已更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM