Solution 1 :
Fragment lifecycle is slightly different from activity, most notably you have to break down view initialization because it has separate lifecycle (unlike activity it can be detached and undergo view destruction while leaving fragment itself intact). For that you should use onViewCreated
method and implement onDestroyView
to clear view references.
Best practice inside fragment to obtain context is to use requireContext()
then you can proceed to pull applicationContext
from it if needed.
Also fragment doesn’t have setContentView
method. You have to either override onCreateView
method or if you’re using static layout you can use Fragment (int contentLayoutId)
constructor instead:
class HomeFragment : Fragment(R.layout.activity_main) {
private var alphaAdapters: AlphaAdapters? = null
private var charItem: ArrayList<CharItem>? = null
private var recyclerView: RecyclerView? = null
private var gridLayoutManager: GridLayoutManager? = null
// initialize adapter in onCreate, it's unaffected by views state
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
charItem = ArrayList()
charItem = setAlphas()
alphaAdapters = AlphaAdapters(requireContext(), charItem!!)
}
// initialize recyclerView and layout manager
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
gridLayoutManager = GridLayoutManager(requireContext(), 2, LinearLayoutManager.VERTICAL, false)
recyclerView = view.findViewById(R.id.recycler_view_item).also {
it.layoutManager = gridLayoutManager
it.setHasFixedSize(true)
it.adapter = alphaAdapters
}
}
// release recyclerView and layout manager
override fun onDestroyView() {
super.onDestroyView()
recyclerView?.adapter = null
recyclerView = null
gridLayoutManager = null
}
private fun setAlphas(): ArrayList<CharItem> {
// (....)
}
}
Problem :
I am new with using GridLayoutManager and need some help with its initializing. I created test version of recyclerview with cardview and it worked perfectly when my code was placed in mainactivity. But now when I copied it to main project to my HomeFragment there was shown errors with GridLayout manager initializing.
my code in HomeFragment:
class HomeFragment : Fragment() {
private var recyclerView: RecyclerView? = null
private var charItem: ArrayList<CharItem>? = null
private var gridLayoutManager: GridLayoutManager? = null
private var alphaAdapters: AlphaAdapters? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
applicationContext
recyclerView = findViewById(R.id.recycler_view_item)
gridLayoutManager =
GridLayoutManager(**applicationContext**, 2, LinearLayoutManager.VERTICAL, false)
recyclerView?.layoutManager = gridLayoutManager
recyclerView?.setHasFixedSize(true)
charItem = ArrayList()
charItem = setAlphas()
alphaAdapters = AlphaAdapters(**applicationContext,** charItem!!)
recyclerView?.adapter = alphaAdapters
}
private fun setAlphas(): ArrayList<CharItem> {
var arrayList: ArrayList<CharItem> = ArrayList()
arrayList.add(CharItem(R.drawable.image_3__2_, R.drawable.ic_nail,"Nail
услуги")).....
}
Problem is in application context
So the problem is in HomeFragment code especially with ApplicationContext in GridLayoutManager. What is the solution? Is there anything which can help me?