I'm making a app
Android that displays an address list and a map with these addresses.
Vertically, the two fragments appear in tabs within a viewPager
.
/res/layout/activity_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:id="@+id/mainContainerV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
tools:context=".MainActivity"
>
<LinearLayout
android:id="@+id/myContainerV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/adView"
android:layout_alignParentTop="true"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarV"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cardview_light_background"
android:theme="@style/ThemeOverlay.AppCompat"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpagerV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center|bottom"
ads:adSize="BANNER"
ads:adUnitId="XXXXXX" />
</RelativeLayout>
</layout>
and horizontally, there are two panels that appear side by side:
/res/layout-land/activity_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:id="@+id/mainContainerH"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/myContainerH"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:fitsSystemWindows="true"
android:orientation="horizontal">
<fragment
android:id="@+id/runPanel"
android:name="br.com.cadima.motonoix.fragments.ListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_recycler" />
<fragment
android:id="@+id/markersMapPanel"
android:name="br.com.cadima.motonoix.fragments.MarkersFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_map"
/>
</LinearLayout>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="XXXXXXX" />
</RelativeLayout>
</layout>
I'm trying to use databinding and for this, in onCreate
of MainActivity
, the binding variable is generated:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
if (getResources().getConfiguration().orientation == ORIENTATION_LANDSCAPE) {
// se o layout estiver na horizontal...
} else {
// se estiver na vertical, ajusta o 'viewPager'
setupViewPager(activityMainBinding.viewpagerV);
tabLayout.setupWithViewPager(viewPager);
}
}
The problem is that when both layouts are present in the project, only the /layout-land
folder is accessible by activityMainBinding
. and these last lines give error because the symbol can not be solved.
} else {
// se estiver na vertical, ajusta o 'viewPager'
setupViewPager(activityMainBinding.viewpagerV);
tabLayout.setupWithViewPager(viewPager);
}
When you delete the layout horizontally, vertical layout views are accessible.
How do you use the databinding with different layouts?
UPDATE: app
is already working. I want to improve the performance and so I decided to adopt the data binding.
I've browsed before asking and saw some broken branches, like using a single layout.xml and disable views by code, but I have several layouts according to orientation and language ... Imagine the nightmare of having to keep this all by code.
This layout does not have an associated data model, but the fragments it will show will have. And because the app has multiple languages and different layouts for screen size, an easy way to perform data binding is essential.
Maybe it's better to go for an alternative like butter knife.
The most important thing is to improve performance and as data binding goes through the UI once, it gives an interesting gain.