我正在与this network graph library合作。
我的布局中有一个GraphSurfaceView
。
此view
通过给它提供一个NetworkGraph
对象来显示图形,如下所示:
graphSurface.init(networkGraph)
如何在MVVM体系结构中将networkGraph
数据绑定到graphSurface
?
我在下面尝试了此代码,但没有用,创建init
后应该调用networkGraph
:
BindingUtils.kt
@BindingAdapter("networkGraph")
@JvmStatic
fun setNetworkGraph(view: GraphSurfaceView, networkGraph: NetworkGraph) {
view.init(networkGraph)
}
myActivity.kt
relationActivityBinding = DataBindingUtil.setContentView(this, getLayoutId())
graphSurface = relationActivityBinding.graphSurface
viewModel.kt
fun getNetworkGraph() = this.graphManager?.getGraphNetwork() // returns my networkGraph
activity.xml
<giwi.org.networkgraph.GraphSurfaceView
android:id="@+id/graphSurface"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:networkGraph="@{viewModel.getNetworkGraph()}"/>