首页 > 解决方案 > 当布局拆分为多个文件时,如何将视图模型添加到布局?

问题描述

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

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();

    }

活动地图.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>

然后就像您看到的那样,将 databindig 添加到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);

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

任何的想法?

谢谢

标签: androidandroid-layoutandroid-databindingandroid-mvvm

解决方案


您必须为您的<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()


推荐阅读