当布局在几个文件中被分割视图模型如何可以添加到布局?

问题描述 投票:0回答:1

我有几个文件的布局分割,这一活动是与抽屉设计的活动,那么我的下一个文件:

map activity.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>

然后,就像你可以看到添加的数据绑定到我把下面的代码Java文件中activity_main.xml中:

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

但是,如何才能添加到为获取访问的属性和功能的结合?

任何想法?

谢谢

android android-layout android-databinding android-mvvm
1个回答
1
投票

你有一个ID设置为您<include>标签。那么你就可以访问它。

举例来说,如果你这样做

<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()访问它

© www.soinside.com 2019 - 2024. All rights reserved.