Home > Mobile >  Linear layout in android Fragment
Linear layout in android Fragment

Time:02-23

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
    }
}

CodePudding user response:

You can use activity or requireActivity

  • Related