I have a layout splited in several files, this activity is an activity with drawer layout, then I have the next files:
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>
Then like you can see for add the databindig to the activity_map.xml I was put the next code in the Java file:
ActivityMapBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_map);
mapViewModel =
ViewModelProviders.of(this).get(MapViewModel.class);
binding.setMapViewModel(mapViewModel);
binding.setLifecycleOwner(this);
For do it in the app_bar_nav.xml I put the next code in the Java file:
NavHeaderMapBinding headerBinding = NavHeaderMapBinding.inflate(getLayoutInflater());
headerBinding.setMapViewModel(mapViewModel);
binding.navView.addHeaderView(headerBinding.getRoot());
but what do I have to do to add it to the content?
I get the content and set the mapViewModel with the next code:
ContentMapBinding contentMapBinding = ContentMapBinding.inflate(getLayoutInflater());
contentMapBinding.setMapViewModel(mapViewModel);
But how can add this to binding for get acces to the attributes and functions?
Any idea?
Thanks
You have to set an id to your <include>
tag. then you can access it.
For instance, if you do this
<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"/>
You will be able to access it with headerBinding.content.setMapViewModel()