当布局拆分为多个文件时,如何将视图模型添加到布局?

How can add viewmodel to layout when the layout is split in several files?

我有一个布局分成几个文件,这个 activity 是一个 activity 抽屉布局,然后我有下一个文件:

MapActivity.java

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

        ActivityMapBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_map);
        mapViewModel = ViewModelProviders.of(this).get(MapViewModel.class);
        binding.setMapViewModel(mapViewModel);
        binding.setLifecycleOwner(this);

        NavHeaderMapBinding headerBinding = NavHeaderMapBinding.inflate(getLayoutInflater());
        headerBinding.setMapViewModel(mapViewModel);
        binding.navView.addHeaderView(headerBinding.getRoot());

        ContentMapBinding contentMapBinding = ContentMapBinding.inflate(getLayoutInflater());
        contentMapBinding.setMapViewModel(mapViewModel);


        setDrawerLayout();
        setNavigationView();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setMap();

    }

activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>

        <variable
            name="mapViewModel"
            type="com.myapp.map.MapViewModel" />
    </data>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <include
            layout="@layout/app_bar_map"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:menu="@menu/activity_map_drawer" />

    </android.support.v4.widget.DrawerLayout>
</layout>

app_bar_nav.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>

        <variable
            name="mapViewModel"
            type="com.myapp.map.MapViewModel" />
    </data>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".map.MapActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorWhite"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


    </android.support.design.widget.CoordinatorLayout>
</layout>

content_map.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="mapViewModel"
            type="com.myapp.map.MapViewModel" />
    </data>
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true">

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            tools:context=".alert.MapActivity" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabCurrentPosittion"
            style="@style/floating_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            app:srcCompat="@drawable/ic_icon_gps"
            android:onClick="@{()-> mapViewModel.setCurrentPosition()}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabDeaList"
            style="@style/floating_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="16dp"
            app:srcCompat="@drawable/ic_icon_listado_deas"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fabCurrentPosittion" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fablert"
            style="@style/floating_alert_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="24dp"
            app:srcCompat="@drawable/ic_icon_campana"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent" />

    </android.support.constraint.ConstraintLayout>

</layout>

然后就像你看到的那样,将数据绑定添加到 activity_map.xml 我将下一个代码放在 Java 文件中:

ActivityMapBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_map);
        mapViewModel = 
ViewModelProviders.of(this).get(MapViewModel.class);
        binding.setMapViewModel(mapViewModel);
        binding.setLifecycleOwner(this);

为了在 app_bar_nav.xml 中执行,我将下一个代码放在 Java 文件中:

NavHeaderMapBinding headerBinding = NavHeaderMapBinding.inflate(getLayoutInflater());
        headerBinding.setMapViewModel(mapViewModel);
        binding.navView.addHeaderView(headerBinding.getRoot());

但是我需要做什么才能将其添加到内容中?

我获取内容并使用下一个代码设置 mapViewModel:

ContentMapBinding contentMapBinding = ContentMapBinding.inflate(getLayoutInflater());
        contentMapBinding.setMapViewModel(mapViewModel);

但是如何将其添加到绑定中以获得对属性和函数的访问权限?

有什么想法吗?

谢谢

您必须为您的 <include> 标签设置一个 ID。然后就可以访问了。

例如,如果你这样做

<include layout="@layout/content_map"
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

您将可以通过 headerBinding.content.setMapViewModel()

访问它