0
I’m making a app
Android that displays a list of addresses and a map with these addresses.
Vertically, the two fragments appear on tabs inside 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 horizontal 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 am trying to use databinding and for this, on onCreate
of MainActivity
, the variable of Binding 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 the two layouts are present in the project, only the folder /layout-land
is accessible by activityMainBinding
. and these last lines give error because the symbol cannot be solved.
} else {
// se estiver na vertical, ajusta o `viewPager`
setupViewPager(activityMainBinding.viewpagerV);
tabLayout.setupWithViewPager(viewPager);
}
When I delete the horizontal layout, views from the vertical layout are accessible.
How to use databinding with distinct layouts?
UPDATE: THE app
is already working. I want to improve the performance and so I decided to adopt the data.
I’ve researched before asking and seen some branch breakers, how to use 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 it all by code.
This layout does not have an associated data model, but the fragments it will show will. And because the app has multiple languages and different layouts for screen size, an easy way to realize the data is essential.
Maybe it is better to leave for an alternative as Butter Knife.
The most important thing is to improve performance and as the Binding data crosses the UI once, it gives an interesting gain.
What version of Gradle you are using?
– ramaral
classpath 'com.android.tools.build:Gradle:2.2.3'
– Rene Freak