android 片段中的线性布局
Linear layout in android Fragment
我想创建一个变量来保存我的 View LinearLayoutManager,但我使用的是底部导航栏,我得到的是片段而不是视图,我不知道如何获取上下文。
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
}
}
您可以使用activity or requireActivity
我想创建一个变量来保存我的 View LinearLayoutManager,但我使用的是底部导航栏,我得到的是片段而不是视图,我不知道如何获取上下文。
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
}
}
您可以使用activity or requireActivity