通过数据绑定初始化Surface View

时间:2019-05-27 05:14:48

标签: android kotlin android-databinding

我正在与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()}"/>

0 个答案:

没有答案