如何在使用 cardview 的片段中初始化 gridlayoutmanager?
How to initialize gridlayoutmanager in fragment which uses cardview?
我刚开始使用 GridLayoutManager,在初始化方面需要一些帮助。我用 cardview 创建了 recyclerview 的测试版本,当我的代码放在 mainactivity 中时它工作得很好。但是现在当我将它复制到主项目到我的 HomeFragment 时,GridLayout 管理器初始化时显示错误。
我在 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
услуги")).....
}
问题出在应用程序上下文中
所以问题出在 HomeFragment 代码中,尤其是 GridLayoutManager 中的 ApplicationContext。解决办法是什么?有什么可以帮助我的吗?
Fragment 生命周期与 activity 略有不同,最值得注意的是您必须分解视图初始化,因为它具有单独的生命周期(与 activity 不同,它可以分离并在保留 fragment 本身的同时进行视图销毁完整)。为此,您应该使用 onViewCreated
方法并实施 onDestroyView
来清除视图引用。
在片段中获取上下文的最佳做法是使用 requireContext()
然后您可以根据需要继续从中提取 applicationContext
。
片段也没有 setContentView
方法。您必须覆盖 onCreateView
方法,或者如果您使用的是静态布局,则可以改用 Fragment (int contentLayoutId)
构造函数:
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> {
// (....)
}
}
我刚开始使用 GridLayoutManager,在初始化方面需要一些帮助。我用 cardview 创建了 recyclerview 的测试版本,当我的代码放在 mainactivity 中时它工作得很好。但是现在当我将它复制到主项目到我的 HomeFragment 时,GridLayout 管理器初始化时显示错误。
我在 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
услуги")).....
}
问题出在应用程序上下文中
所以问题出在 HomeFragment 代码中,尤其是 GridLayoutManager 中的 ApplicationContext。解决办法是什么?有什么可以帮助我的吗?
Fragment 生命周期与 activity 略有不同,最值得注意的是您必须分解视图初始化,因为它具有单独的生命周期(与 activity 不同,它可以分离并在保留 fragment 本身的同时进行视图销毁完整)。为此,您应该使用 onViewCreated
方法并实施 onDestroyView
来清除视图引用。
在片段中获取上下文的最佳做法是使用 requireContext()
然后您可以根据需要继续从中提取 applicationContext
。
片段也没有 setContentView
方法。您必须覆盖 onCreateView
方法,或者如果您使用的是静态布局,则可以改用 Fragment (int contentLayoutId)
构造函数:
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> {
// (....)
}
}