-1

I want to create a variable that holds my View LinearLayoutManager, but I'm using bottom nav bar and I got fragments instead of views and I don't know how to get the context.

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val cardsRecyclerView : RecyclerView = binding.rvCards
        val homeLayout : ConstraintLayout = binding.homeLayout
        homeViewModel.text.observe(viewLifecycleOwner) {

            val linearLayout = LinearLayoutManager(/*here i can't get the context*/)
            val adapter = RecyclerAdapter(mutableListOf("blue", "orange", "pink", "gray"), mutableListOf(6092049204821920, 3958271029482085, 8375027502862095, 2957285928029573)
                , mutableListOf(16,7,13,24), mutableListOf(7,4,1,10), mutableListOf("Eduard Radu", "John Doe", "Ayman Achalhe", "Mike Rivera"))

            cardsRecyclerView.layoutManager = linearLayout

            cardsRecyclerView.adapter = adapter

        }
        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Fovu
  • 3
  • 2
  • loads and loads of resources online on [using context in a fragment](https://stackoverflow.com/questions/8215308/using-context-in-a-fragment) – a_local_nobody Feb 22 '22 at 15:47
  • 1
    Does this answer your question? [Using context in a fragment](https://stackoverflow.com/questions/8215308/using-context-in-a-fragment) – a_local_nobody Feb 22 '22 at 15:47

1 Answers1

-1

You can use activity or requireActivity

GTID
  • 538
  • 2
  • 6
  • 19