The goal is to display two Fragments (Frag A, Frag B) on the screen in a different layout depending on the screen orientation (portrait or landscape).
When the device is in Portrait mode, only one of Frag A and Frag B is displayed at a time using swiping to switch between UIs. ViewPager works perfectly for this.
When the device is in Landscape mode, Frag A is displayed on the left of the screen and Frag B is displayed on the right. A simple LinearLayout works perfectly for this.
The problem arises when trying to get both working in the same application. It seems like the ViewPager class must be referred to in the Java source code (in the onCreate method). This means that a UI element is defined in both the XML and the source code... which seems like a problem when the device is in Landscape mode and there should not be ViewPager object but one is referred to in the source code. Am I approaching this correctly? If one layout is using ViewPager, does every other layout need a ViewPager?
layout/activity_mymain.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyMainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
layout-land/activity_mymain.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.ActionFragment"
android:id="@+id/action_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.SummaryFragment"
android:id="@+id/summary_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
From MyMainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topmain);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
ViewPager is used to instantiate View objects at runtime, so it needs to be defined in the code.
What I was trying to do is very similar to the different tablet/handset layouts described in the Fragments Guide, where they recommend using separate Activities.
(If anyone knows of a way to handle swiping entirely in XML, that would be a much better solution)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With