简体   繁体   English

片段上下文中的 recyclerview 在 kotlin 中不起作用

[英]recyclerview in fragment context not working in kotlin

Im creating a really basic recyclerview in fragment_home.xml which is linked to a <FrameLayout /> inserted in activity_main.xml我在fragment_home.xml中创建了一个非常基本的recyclerview ,它链接到插入到activity_main.xml中的<FrameLayout />

It works well...until I create the HomeFragment class in HomeFragment.kt , which inflates fragment_home.xml它运作良好......直到我在 HomeFragment.kt 中创建HomeFragment HomeFragment.kt ,它膨胀了fragment_home.xml

Then I want to add up the arrayList ( arrayHomeMenu ) in the function onActivityCreated()然后我想在 function onActivityCreated()中添加 arrayList ( arrayHomeMenu )

my problem is with this line我的问题是这条线

  val homeMenuAdapter = HomeMenuAdapter(arrayHomeMenu, this) //context "this" appears with red underline

It retrieves error, so I cant continue...它检索错误,所以我不能继续......

HomeFragment.kt HomeFragment.kt

   class HomeFragment : Fragment() {
   override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
    ): View?{
    return inflater.inflate(R.layout.fragment_home, container, false)

 }


override fun onActivityCreated(savedInstanceState: Bundle?){
    super.onActivityCreated(savedInstanceState)


    //create list of words
    val arrayHomeMenu = ArrayList<HomeMenuModel>()

    arrayHomeMenu.add(HomeMenuModel("Verbs","List of 461  words", R.drawable.ic_logo))
    arrayHomeMenu.add(HomeMenuModel("Nouns","List of 52 words", R.drawable.ic_logo))
    arrayHomeMenu.add(HomeMenuModel("Adjectives","List of 65 words", R.drawable.ic_logo))
    arrayHomeMenu.add(HomeMenuModel("Adverbs","List of 345 words", R.drawable.ic_logo))

    val homeMenuAdapter = HomeMenuAdapter(arrayHomeMenu, this) //context "this" appears with red underline


    homeMenu_recyclerView.layoutManager = LinearLayoutManager(context)
    homeMenu_recyclerView.adapter = homeMenuAdapter

}

This is a context problem....这是一个context问题....

I tried to replace this by activity , context , Context , applicationContext with not success at all...我试图用activitycontextContextapplicationContext替换this ,但根本没有成功......

HomeMenuAdapter.kt HomeMenuAdapter.kt

class HomeMenuAdapter(val arrayList: ArrayList<HomeMenuModel>, val context: Context) :
RecyclerView.Adapter<HomeMenuAdapter.ViewHolder>() {

    ....//correct content
   }

It works correctly when I create the recyclerview direct in activity_main.xml and work directly with MainActivity.kt当我在activity_main.xml直接创建 recyclerview 并直接使用MainActivity.kt时,它可以正常工作

Once I move the recyclerview to a fragment...it throws the error described above.一旦我将 recyclerview 移动到一个片段......它会引发上述错误。

HomeMenuModel.kt HomeMenuModel.kt

  class HomeMenuModel(val hm_title:String,val hm_description: String, val hm_image:Int)

I was checking this answer , but no success...我正在检查这个答案,但没有成功......

what am doing wrong??做错了什么? thank you谢谢你

The reason why you can use this as context inside an Activity, is because Activity extends Context.之所以可以在 Activity 中使用this作为上下文,是因为Activity扩展了 Context。

Fragment however does not extend Context so you cannot use this .但是Fragment不扩展 Context 所以你不能使用this

The reason why activity , context also don't work is Kotlin's distinction of nullable types. activitycontext也不起作用的原因是 Kotlin 对可空类型的区别。 While activity and context do return a Context, the return value is nullable.虽然activitycontext确实返回一个上下文,但返回值是可以为空的。 You can see this by paying close attention to the error message that appears when hovering over the red underline:您可以通过密切注意将鼠标悬停在红色下划线上时出现的错误消息来看到这一点:

Type mismatch.
Required:
Context
Found:
Context? (or FragmentActivity?)

The question mark indicates that this is a nullable type, while a non-nullable Context is required.问号表示这是一个可为空的类型,而一个不可为空的 Context 是必需的。 The reason why they're nullable is that the Fragment can only retrieve the Activity when it is attached to it, which is not always the case.它们可以为空的原因是 Fragment 只能在 Activity 附加到它时检索它,但情况并非总是如此。

However, Fragment has a convenient method called requireContext() to work around this issue.然而,Fragment 有一个方便的方法叫做requireContext()来解决这个问题。 It has a non-nullable return type but will instead throw an exception if it cannot retrieve the context, so it is on you to make sure to only call it when the Fragment is attached.它具有不可为空的返回类型,但如果它无法检索上下文,则会引发异常,因此您必须确保仅在附加 Fragment 时调用它。

In short, you should be able to instantiate your adapter like this:简而言之,您应该能够像这样实例化您的适配器:

val homeMenuAdapter = HomeMenuAdapter(arrayHomeMenu, requireContext())

Since you are in a Fragment this can't be passed as a context:由于您在 Fragment 中,因此不能将其作为上下文传递:

val homeMenuAdapter = HomeMenuAdapter(arrayHomeMenu, this)

In the above code you are trying to pass fragment as a context, so instead what you can do is get the context of the fragment.在上面的代码中,您尝试将片段作为上下文传递,因此您可以做的是获取片段的上下文。 So you can do:所以你可以这样做:

val homeMenuAdapter = HomeMenuAdapter(arrayHomeMenu, requireContext())

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

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