The documentation suggests to use AndroidViewBinding API for the task.
I intended to learn Compose in my project, but also wanted a LineChart from MPAndroidChart library.
Here is what I did. In the fragment:
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val lineChart = FragmentChartBinding.inflate(inflater)
return ComposeView(requireContext()).apply {
setContent {
SetContent(viewModel, findNavController(), lineChart)
}
}
}
And in the composable:
@Composable
fun setChart(
state: State<List<BodyMeasurement.OxygenMeasurement>>,
lineChart: FragmentChartBinding
) = AndroidViewBinding(
bindingBlock = { _, _, _ ->
lineChart
},
modifier = Modifier
.fillMaxSize(),
update = { refreshChartData(lineChart, state.value) }
)
The samples that I found on the internet dealt with static views, so I wrote this little note. Enjoy!
The quoted code is at GitHub.