科特林后的And​​r​​oid碎片回收空回观 [英] kotlin android fragment empty recycler view after back

查看:207
本文介绍了科特林后的And​​r​​oid碎片回收空回观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的机器人片段:

I have this android fragment:

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)
    }
  }
}                               

一切正常,但后,我回到这个片段很好的(由另一个片段经理取代,加入到backstack)。当我回来,我得到一个错误说: E / RecyclerView:无适配器连接;跳绳布局。我敢打赌,它是与懒惰的属性,但为什么我不能看着办吧。该片段显示在第一次运行良好,回去只有当它没有显示出回收的项目和显示此信息

Everything works good except after i come back to this fragment (replaced in fragment manager by another, added to the backstack). When i return, i get an error saying: E/RecyclerView: No adapter attached; skipping layout. My bet is that its something to do with lazy properties but i can't figure it why. The fragment shows good on first run, only when going back it shows no recycler view items and display this message

推荐答案

所以我的属性被声明为懒{找到&LT;视图&gt;(R.id.xxx} 基本上意味着他们将只能从布局充气一次。如果片段的观点在某种程度上需要重新创建的,则recyclerview属性将指向一个旧观点。

So as my properties are declared as lazy { find<View>(R.id.xxx} that basically means they will only be inflated from the layout one time. If the fragment's view somehow needs to be created again, the recyclerview property will point to an old view.

我不知道如何在这种情况下,一个空指针异常被抛出没有。简单地改变懒到 VAR ,并重新分配给它的 onCreateView 。另一件事,如果我们要使用相同的的LayoutManager 新充气recyclerview,我们必须从previous recyclerview清除 recyclerView.layoutManager = NULL ,否则,将引发异常说是的layoutManager 已经绑定到另一个recyclerView。

I'm not sure how a null pointer exception in that case was not thrown. Simply change the lazy to a var, and re-assign it on onCreateView. Another thing, if we want to use the same LayoutManager for the new inflated recyclerview, we must clear it from the previous recyclerview recyclerView.layoutManager = null, otherwise, an exception will be thrown saying that that layoutManager is already binded to another recyclerView.

这篇关于科特林后的And​​r​​oid碎片回收空回观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆