我有一个片段,其中有MPAndroidChart和textViews。我知道MPAndroidChart不支持这里指出的数据绑定:https://github.com/PhilJay/MPAndroidChart/issues/1382
现在,我正在使用ButterKnife查找我的视图,但希望迁移到数据绑定。我想知道既然MPAndroidChart不支持数据绑定,有没有可能在同一个片段中同时使用数据绑定和蝶形刀呢?
我的文本视图的MPAndroidChart和DataBinding的小刀。
发布于 2018-09-18 18:41:15
是的,这是可能的,
蝶形刀和数据绑定是两个不同的东西。
而是为什么你想要奶油刀。见下文
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pieChart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">您可以像这样访问此字段
ActivityMainBinding binding;
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.pieChart.setVisibility(Visibility.INVISIBLE)因此,当您使用数据绑定时,不需要使用小刀。
发布于 2018-09-18 18:58:59
您可以通过以下代码1将PieChart与数据绑定一起使用。
setPieChart(fragmentDashboardBinding.piechart);声明饼图变量global
private PieChart piechart;
public void setPieChart(PieChart Pie_chart) {
this.piechart = Pie_chart;
}并在PieChart表单更新方法上设置数据
@Override
public void update(Observable observable, Object arg) {
DashboardViewModel dashboardViewModel = (DashboardViewModel) observable;
drivingScore = dashboardViewModel.getDrivingScore();
LoadPieChart(drivingScore);
}
private void LoadPieChart(DrivingScore drivingScore) {
//Do any thing with your piechart
}发布于 2018-09-18 19:13:53
可以将MPAndroidChart与数据绑定一起使用。
创建自定义绑定适配器:
@BindingAdapter("android:setPieData")
fun setPieData(view: PieChart, data: List<SomeModel>?) {
val entries = mutableListOf<PieEntry>()
data.forEach { sweet ->
// Fill the entires list
}
// Set the entries, decorate the pie
}然后在xml中:
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/consumed_data_sweets_rating_chart"
android:layout_width="match_parent"
android:layout_height="@dimen/chart_height"
android:layout_gravity="center"
android:layout_margin="@dimen/material_component_cards_top_and_bottom_padding"
android:setPieData="@{viewmodel.pieData}"其中viewmodel.pieData是在ViewModel类中传入的LiveData<List<SomeModel>> models。
因此,基本上您必须为要设置的任何图表属性创建自定义数据绑定适配器。
Link to my project,查找ChartBindingAdapters类以查看我的自定义适配器。
https://stackoverflow.com/questions/52046507
复制相似问题