I’m learning Kotlin
in Adroid Studio and recently I have stuck on RecyclerView
.
The problem is that I’m not able to go forward due to error showing on LinearLayoutManager(this)
.
My little code looks like:
package com.store.example
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_store.*
class StoreFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// call recyclerView this id (fragment_store)
recyclerView.layoutManager = LinearLayoutManager(this)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_store, container, false)
}
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StoreFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/card_view_store" />
</FrameLayout>
LinearLayoutManager(this)
is underlined and tells me that:
Type mismatch: inferred type is StoreFragment but Context! was expected
I’m not a advanced programist, rather a newbie and I’ve been trying to fix this remark but at this moment its beyond my skills.
Thank you for your help.
4
Answers
Try this instead,
or
activity returns fragment`s host activity whereas requireContext() returns the context of the fragment`s hosting activity.
this
refers to theFragment
(StoreFragment
to be precise), but the constructor forLinearLayoutManager
needs a reference to an instance ofContext
.You have a number of different ways to get a reference to a
Context
.this.context
will return the context property of the fragment. Can omitthis.
and just usecontext
to do the same thing.view.context
will return the context property of the view argument.requireActivity()
will return the activity that controls the fragment which can also be used as an instance of Context.Use
requireContext()
you can get the fragment’s hosting activity.So you can try this:
Just to add to the answers you’ve been given,
Context
parameters show up a lot in Android, so you need to know how to be able to get one. AnActivity
is aContext
, which is why if your code were in an Activity, you could passthis
.But you’re in a
Fragment
, which isn’t a subclass ofContext
, sothis
won’t work! ButFragment
s do have access to aContext
– either throughgetContext
(or in Kotlin, thecontext
property equivalent) orrequireContext
.getContext
returns null if you don’t have one yet, meaning you have to handle that nullability.requireContext
throws an exception if you don’t have access to one, so it doesn’t need to be nullable, and this is the one you’re supposed to use. So long as you callrequireContext
inonCreate
or later, the Fragment will have been attached to its context and it’s safe to use.