Wednesday, 15 May 2013

Android: How do you mix ViewPager layout with non-ViewPager layout for different device orientations? -



Android: How do you mix ViewPager layout with non-ViewPager layout for different device orientations? -

the goal display 2 fragments (frag a, frag b) on screen in different layout depending on screen orientation (portrait or landscape).

when device in portrait mode, 1 of frag , frag b displayed @ time using swiping switch between uis. viewpager works this.

when device in landscape mode, frag displayed on left of screen , frag b displayed on right. simple linearlayout works this.

the problem arises when trying both working in same application. seems viewpager class must referred in java source code (in oncreate method). means ui element defined in both xml , source code... seems problem when device in landscape mode , there should not viewpager object 1 referred in source code. approaching correctly? if 1 layout using viewpager, every other layout need 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" > <!-- title strip display visible page title, page titles 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 adapter homecoming fragment each of 3 // primary sections of app. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); }

viewpager used instantiate view objects @ runtime, needs defined in code.

what trying similar different tablet/handset layouts described in fragments guide, recommend using separate activities.

(if knows of way handle swiping exclusively in xml, much improve solution)

android android-layout

No comments:

Post a Comment