简体   繁体   English

如何通知 RecyclerView 数据已从适配器内的计时器中更改

[英]How can I inform the RecyclerView that data has changed from within a Timer inside the Adapter

I have a RecyclerView where you can swipe left items to archive them.我有一个 RecyclerView,您可以在其中向左滑动项目以存档它们。 It would first show the undo layout then after 2 seconds it will be archived (now for a first test I just delete the item).它会首先显示撤消布局,然后在 2 秒后将其存档(现在对于第一次测试,我只是删除了该项目)。 The problem is that using notifyItemRemoved(pos) from a Timer thread crashes the app saying that only the original thread that created the view (UI thread) could touch its views.问题是使用 Timer 线程中的notifyItemRemoved(pos)会使应用程序崩溃,说只有创建视图的原始线程(UI 线程)可以触摸其视图。 Is there any trick to go around this? go 有什么技巧吗? I use the timer inside the ViewHolder of my RecyclerView.Adapter extended class.我在我的 RecyclerView.Adapter 扩展 class 的 ViewHolder 中使用计时器。

Here's the code of the timer:这是计时器的代码:

archiveTimer.schedule(new TimerTask() {
    @Override
    public void run() {
        notesController.remove(position);
        notesList.remove(position);
        queuedForArchive = false;
    }
}, 2000);

On activities you can use runOnUiThread but I can't use that either (function MainActivity.runOnUiThread(Runnable) is not static so I can't use it outside MainActivity)在活动中,您可以使用 runOnUiThread 但我也不能使用(函数MainActivity.runOnUiThread(Runnable)不是 static 所以我不能在 MainActivity 之外使用它)

I found a really tricky way of making timed events happen on UI thread without using a timer.我发现了一种非常棘手的方法,可以在不使用计时器的情况下在 UI 线程上发生定时事件。 I noticed that all animators are actually timed and you can impl onAnimationEnd on any Animator you want, so I made an ObjectAnimator that changes alpha of an invisible view from 1 to 1 (no change) and set its duration to 2000ms that I needed.我注意到所有动画师实际上都是定时的,你可以在任何你想要的动画师上实现 onAnimationEnd,所以我制作了一个 ObjectAnimator,它将不可见视图的 alpha 从 1 更改为 1(没有变化)并将其持续时间设置为我需要的 2000 毫秒。 I used the same code I wanted to use in the timer inside onAnimationEnd() of that Animator and it worked!!!我使用了我想在该动画器的 onAnimationEnd() 内的计时器中使用的相同代码,它工作了!!! All code was run on UI thread and changes to other layouts did not create any crashes!所有代码都在 UI 线程上运行,对其他布局的更改不会造成任何崩溃!

暂无
暂无

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

相关问题 Android:通知适配器数据集已从静态方法更改 - Android : Inform adapter that data-set has changed from a static method 在这种情况下,我如何通知适配器数据已被更改/删除 - how can i notify the adapter that data has been changed/deleted in THIS scenario 如何使用 AsyncTask、RecyclerView 和 RecyclerView.Adapter 将手机中的图像获取到 RecyclerView? - How can I use AsyncTask, RecyclerView, and a RecyclerView.Adapter to get images from my phone into my RecyclerView? 如何从RecyclerView Adapter传递给MainActivity的变量? - How can I get a variable from RecyclerView Adapter passed to MainActivity? 单击项目后,如何从RecyclerView适配器内部的drawable更改imageView的图像? - How to change image of imageView from drawable inside an adapter of RecyclerView after an item has been clicked? 当我有两个recyclerview数据源,但是只有一个数据源被更改时,如何在recyclerview中使用notifyDataSetChanged() - How to use notifyDataSetChanged() in recyclerview, when I have two data sources for the recyclerview, but only one data source has been changed 如何在 recyclerview 适配器中为 Activity 扩展上下文 - How do I extablish context for an Activity within a recyclerview adapter Eclipse插件开发:如何通知视图内容已更改? - Eclipse plugin development: How to inform view that content has changed? 从适配器内部刷新整个RecyclerView - refresh the whole RecyclerView from within the Adapter 如何关闭 recyclerview 适配器中的 localbroadcast 侦听器? 有没有更好的方法来与从它调用的活动进行通信? - How can I turn off localbroadcast listener in recyclerview adapter? Of is there a better way to communicate with an activity called from it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM