Android:两种方式绑定对象扩展视图的数据

时间:2018-10-19 01:56:25

标签: android mvvm android-mvvm

尝试与我的Editor类(在模型中)进行双向数据绑定时遇到麻烦。该对象扩展了View并在布局中直接调用。 问题是,如何实现两种方式的数据绑定,所以当我在EditionViewModel中更改Editor时,它在视图中也会更改吗? (我的班级编辑器代表要在其上绘制的画布)

这是我的EditionViewModel代码:

public class EditionViewModel extends ViewModel {
private Editor editor;
private User user;
private SocketService socketService;
private EditionErrorHandler editionErrorHandler;

public EditionViewModel(User user, SocketService socketService, EditionErrorHandler editionErrorHandler, Editor editor) {
    this.user = user;
    this.socketService = socketService;
    this.editionErrorHandler = editionErrorHandler;
    this.editor = editor;
}


public Editor getEditor() {
    return editor;
}

public void setEditor(Editor editor) {
    this.editor = editor;
}

public void onPenClicked(View view){
    editor.normal();
}

public void onPenEraserClicked(View view){
    editor.eraser();
}

这是EditionActivity:

public class EditionActivity extends AppCompatActivity implements EditionErrorHandler {

private Editor edition;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActivityEditionBinding activityEditionBinding = DataBindingUtil.setContentView(this, R.layout.activity_edition);
    edition = (Editor) findViewById(R.id.edition);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    edition.init(metrics);
    activityEditionBinding.setViewModel(ViewModelProviders.of(
            this,
            new EditionViewModelFactory(null, null, this, edition))
            .get(EditionViewModel.class));

}

最后是布局edition_activity:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <variable
        name="viewModel"
        type="ca.polymtl.equipe5.polypaint.viewmodel.EditionViewModel" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ca.polymtl.equipe5.polypaint.view.EditionActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:background="#aea1a1">
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:drawableTop="@drawable/crayon"
            android:onClick="@{viewModel::onPenClicked}" />
        </LinearLayout>
    <ca.polymtl.equipe5.polypaint.model.Editor
        android:id="@+id/edition"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
        <!--android:src="@={viewModel.Editor}"-->
        />
</LinearLayout>

有趣的部分是在ca.polymtl.equipe5.polypaint.model.Editor TAG处。 在这一点上,我如何实现双向数据绑定,以使我的Editor对象与EditionViewModel的Editor对象相同? (我通过android:src =“ @ = {viewModel.Editor}”并通过直接使用Edition_Activity中的ID直接找到编辑器进行了a脚的尝试)

感谢您提供的任何帮助。

0 个答案:

没有答案