使用数据绑定时导航栏出现 2 次

Navbar appear 2 times when use databinding

您好,我正在尝试将 data-bitindng 与导航栏一起使用,我有下一个代码:

MapActivity.java

public class MapActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback, GoogleMap.OnMarkerClickListener{

    private static final float INITIAL_MAP_ZOOM_LEVEL = 17;
    private static final int INITIAL_REQUEST_CODE = 1;
    private GoogleMap mMap;
    private CameraUpdate cameraUpdate = null;
    private MapViewModel mapViewModel;

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

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

    }
...

map_layout.xml

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

        <variable
            name="mapViewModel"
            type="com.emtmadrid.cardiomadapp.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:headerLayout="@layout/nav_header_map"
            app:menu="@menu/activity_map_drawer" />

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

app_nav_bar.xml

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

        <variable
            name="mapViewModel"
            type="com.emtmadrid.cardiomadapp.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/white"
                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>

当执行这段代码时,我得到了 MapActivity 的屏幕,但是有 2 个导航栏,第一个导航栏数据绑定不起作用,第二个导航栏工作正常。

如何解决这个问题?

有什么想法吗?

谢谢。

为了解决这个问题,我在 app_nav_bar.xml

中删除了以下行
app:headerLayout="@layout/nav_header_map"

我想质疑已接受的答案,因为删除 app:headerLayout 对我来说似乎适得其反......数据绑定只是缺少 [=14] 上的 bind 属性=]节点:

<include
    layout="@layout/app_bar_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    bind:mapViewModel="@{mapViewModel}"/>

同样适用于此 include,应移至 app_nav_bar.xml 之外:

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

结果将是一个有效的数据绑定,其中只需分配一次 MapViewModelbind 的名称-space 将是 xmlns:bind="http://schemas.android.com/apk/res-auto".

app_nav_bar.xml只包含AppBarLayout时,可能更容易处理;特别是 include layout="@layout/content_map" 在一个名为 app_nav_bar.xml 的文件中显得相当混乱。当已经用 app:headerLayout 分配时不需要膨胀 NavHeaderMapBinding - 但需要 bind 属性才能绑定它。

Data-Binding Expressions: Includes 还解释了 includebind 的组合——这通常比通过代码膨胀和绑定多个视图更省力——因为膨胀和绑定视图 只有一次 工作,当include 节点与布局资源XML 正确数据绑定时。这是数据绑定的真正优点,不必通过代码绑定所有包含项。