Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – How to initialize gridlayoutmanager in fragment which uses cardview?

Posted on November 14, 2022

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

READ  [ANSWERED] android - Detect if Material You is present in device
Powered by Inline Related Posts

So the problem is in HomeFragment code especially with ApplicationContext in GridLayoutManager. What is the solution? Is there anything which can help me?

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress