android - Multi-Pane fragments within viewpager -
i'm working on app act "ebook" player. having read through number of tutorials related fragments, decided take 'fragment/ viewpager' approach.
moving little more detail, decided follow great tutorial on nested fragments linden darlinghttps://plus.google.com/100467024733771542884/posts/24hcfw5heiv.
i created 'webview' fragment each individual book page, it's own layout. if book consists of 6 pages, i'll have bookpage1fragment, bookpage2fragment, bookpage3fragment, bookpage4fragment, bookpage5fragment, bookpage6fragment etc. managed working within viewpager, swiping through fragments etc working fine. problem is, 1 fragment @ time visible on screen. i'd have 2 fragments (multi-pane), when on running app on large devices , landscape orientation , single-pane (one fragment @ time) when running on smaller devices or portrait view. want display 2 pane layout 1 fragment left , 1 fragment right.
see code below:
public class parentfragment extends fragment { public static final string tag = parentfragment.class.getsimplename(); public static parentfragment newinstance() { return new parentfragment(); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setretaininstance(true); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_parent, container, false); } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); viewpager mviewpager = (viewpager) view.findviewbyid(r.id.viewpager); mviewpager.setadapter(new myadapter(getchildfragmentmanager())); } public static class myadapter extends fragmentpageradapter { public myadapter(fragmentmanager fm) { super(fm); } @override public int getcount() { return 6; } /* @override public fragment getitem(int position) { bundle args = new bundle(); args.putint(textviewfragment.position_key, position); return textviewfragment.newinstance(args); } */ @override public fragment getitem(int position) { switch (position) { case 0: return new bookpage1fragment(); case 1: return new bookpage2fragment(); case 2: return new bookpage3fragment(); case 3: return new bookpage4fragment(); case 4: return new bookpage5fragment(); case 5: return new bookpage6fragment(); default: return getitem(0); } } @override public charsequence getpagetitle(int position) { return "fragment # " + position; } } }
basically 2 fragments appearing on landscape mode , 1 fragment appearing @ time when in portrait mode, example describes on developer.blogspot http://android-developers.blogspot.in/2011/02/android-30-fragments-api.html, embedded within viewpager.
here main.layout.
<?xml version="1.0" encoding="utf-8"?> <relativelayout> xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> </relativelayout>
here webview fragment
public class webviewfragment extends fragment { @suppresslint("setjavascriptenabled") public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view mainview = (view) inflater.inflate(r.layout.fragment_web, container, false); webview mywebview = (webview) mainview.findviewbyid(r.id.webview); mywebview.loadurl("file:///android_asset/ebook-001.html"); mywebview.setwebviewclient(new mywebviewclient()); mywebview.getsettings().setbuiltinzoomcontrols(true); mywebview.getsettings().setjavascriptenabled(true); mywebview.getsettings().setsupportzoom(true); mywebview.getsettings().setjavascriptcanopenwindowsautomatically(true); mywebview.getsettings().setallowfileaccess(true); mywebview.getsettings().setdomstorageenabled(true); mywebview.getsettings().setusewideviewport(true); mywebview.getsettings().setloadwithoverviewmode(true); return mainview; } }
here webview fragment layout:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <webview android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </linearlayout>
so in conclusion i'd to:
1: display 2 fragments within viewpager in landscape mode, , single in portrait mode.
2: use 1 activity regardless of device size, , decide @ runtime whether combine fragments in layout (to create multiple-pane design) or swap fragments (to create single-pane design)
hope makes sense! thank in advance suggestions...
you have create layout file landscape mode.you have create file in res folder layout-land name.in folder create layout same name placed in layout file.android automatically access when screen change landscape mode.
Comments
Post a Comment