Android RecyclerView-notifyDataSetChanged仅在延迟1毫秒后工作

Android RecyclerView-notifyDataSetChanged仅在延迟1毫秒后工作,android,android-recyclerview,kotlin,layout-inflater,notifydatasetchanged,Android,Android Recyclerview,Kotlin,Layout Inflater,Notifydatasetchanged,我正在创建一个自定义键盘应用程序,它使用InputMethodService。 我注意到一些非常奇怪的事情 如果我调用mainview.tabRecyclerView.adapter.notifyDataSetChanged()(mainview.tabRecycler是一个回收器视图,由于Kotlin扩展,我可以这样引用它)在被重写的onCreateInputView方法内部,那么什么也不会发生 下面是一个什么都没发生的例子: override fun onCreateInputView():

我正在创建一个自定义键盘应用程序,它使用
InputMethodService
。 我注意到一些非常奇怪的事情

如果我调用
mainview.tabRecyclerView.adapter.notifyDataSetChanged()
mainview.tabRecycler
是一个回收器视图,由于Kotlin扩展,我可以这样引用它)在被重写的
onCreateInputView
方法内部,那么什么也不会发生

下面是一个什么都没发生的例子:

override fun onCreateInputView(): View {

    mainview = layoutInflater.inflate(R.layout.keyboardmain, null)

    //Insert code that manipulates the variable called tabs, which is just a List

    mainview.tabRecyclerView.adapter = TabAdapter(tabs, this)

    mainview.tabRecyclerView.adapter.notifyDataSetChanged()

    return mainview
}
但是,如果我以至少1毫秒的延迟从一个协程中调用
mainview.tabRecyclerView.adapter.notifyDataSetChanged()
,则
recyclerview
“tabRecyclerView”将显示视图

以下是一个例子:

override fun onCreateInputView(): View {

    mainview = layoutInflater.inflate(R.layout.keyboardmain, null)

    //Insert code that manipulates the variable called tabs, which is just a List

    mainview.tabRecyclerView.adapter = TabAdapter(tabs, this)

launch(UI) {
           //The delay is important, it needs to be here
           delay(1)
            mainview.tabRecyclerView.adapter.notifyDataSetChanged()
        }

    return mainview
}
我怀疑
mainview.tabRecyclerView.adapter.notifyDataSetChanged()
只会在方法
onCreateInputView
完成并返回膨胀的
mainview
对象后刷新
tabRecyclerView

由于这种奇怪的行为,我不得不创建一个延迟至少为1毫秒的协同路由,以便更新适配器的数据集

这似乎不是更新我的recyclerview数据集的好方法

我能做些什么来避免这种行为


虽然我的帖子使用了Kotlin中的示例,但Java中的答案也不错

@MarkKeen何时开始充气是异步的?文件中没有提到这一点。您能提供任何说明您所说的共享的参考吗?删除了注释,感谢@pskink确认我是错误的。@pskink设置适配器而不调用
notifyDataSetChanged()
没有任何作用。我相信[A]适配器必须在
onCreateInputView
方法完成后设置,或者[B]如果适配器是在
onCreateInputView
完成之前设置的,那么
notifyDataSetChanged()
必须在
onCreateInputView
完成后调用。@pskink我知道我可以直接调用
setAdapter()
,但由于某些原因,如果在
onCreateInputView()
尚未完成时调用它,则
setAdapter()
无法工作。我需要调用
setAdapter()
一次
onCreateInputView()
完成运行并返回对象
mainview
。不幸的是,我无法在协同程序中调用
setAdapter()
,因此我被迫在
onCreateInputView()中调用
setAdapter()
,并使用协同程序调用
notifyDataSetChanged()
,延迟1毫秒,所以我知道,
onCreateInputView
已经finished@pskink似乎在
onCreateInputView
之后调用了
onStartInputView
。因此,在该方法中设置适配器是可行的。但是,查看该方法的文档,似乎这不是设置适配器/use
notifyDataSetChanged()
的合适位置。