The goal is to display two fragments (Frag A, Frag B) on the screen in a different layout depending on the orientation of the screen (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 user interfaces. ViewPager works great for this.
When the device is in landscape mode, Frag A is displayed on the left side of the screen, and Frag B is displayed on the right. Simple LinearLayout works great for this.
The problem occurs when trying to work as in a single application. It seems that the ViewPager class should be mentioned in the Java source code (in the onCreate method). This means that the user interface element is defined both in XML and in the source code ... which seems to be a problem when the device is in landscape mode and there should not be a ViewPager object, but the source code is indicated in it. Am I right about this? If one layout uses ViewPager, is any other ViewPager layout needed?
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" >
<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 Earth / 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);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
source
share