我有這樣的機器人片段:科特林Android的片段空回收視圖後回
class MainFragment: BaseFragment(){
private val recyclerView by lazy { find<RecyclerView>(R.id.recyclerView) }
private val fab by lazy { find<FloatingActionButton>(R.id.fab) }
private val myLayoutManager by lazy { LinearLayoutManager(ctx, LinearLayoutManager.VERTICAL, false) }
private val myAdapter by lazy { MainCardAdapter(ctx, ArrayList<MainCardAdapterItem>(), R.layout.card_main_item) }
override val fragmentLayout = R.layout.fragment_main_layout
val DUMMY_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing"
)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = super.onCreateView(inflater, container, savedInstanceState)
setHasOptionsMenu(true)
return view
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
Log.i("TAG", "onViewCreated")
super.onViewCreated(view, savedInstanceState)
(act as MainActivity).run { showWidgetStars(true) }
recyclerView
.linkToLayoutManager(myLayoutManager)
.linkToAdapter(myAdapter)
.addItemDecorator(removedSwipeLeftDecorator)
.setOnItemChangedDuration(500)
myAdapter.run {
setAdapterItems(dummyList)
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.activity_main_context, menu)
menu.findItem(R.id.action_secundary_menu).run {
isVisible = true
icon.setTintCompat(ctx, act.colorFromRes(R.color.appGreyDark))
}
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.action_secundary_menu -> {
act.showSnackBar("Clicked Secondary Menu!")
return true
}
else -> return super.onOptionsItemSelected(item)
}
}
}
一切正常,只是後,我回到這個片段很好的(由另一個片段取代的經理,加入到堆棧中)。當我返回時,我收到一條錯誤消息:E/RecyclerView: No adapter attached; skipping layout
。我的賭注是,它與懶惰屬性有關,但我不明白爲什麼。該片段在第一次運行時顯示良好,只有在返回後才顯示沒有回收站查看項目並顯示此消息
你可以檢查當你回到片段時是否運行'onViewCreated()'? – AndroidEx
是的,它叫它。 –
你可以嘗試在onResume中設置你的adapterItems嗎? – CaseyB