Reputation: 842
I am implementing an Android layout that acts as an instruction manual of sorts. So basically I have several pages that the user can flip through. Let's say I implemented it like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/page_1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Put stuff for page 1 -->
</LinearLayout>
<LinearLayout
android:id="@+id/page_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="INVISIBLE">
<!-- Put stuff for page 2 -->
</LinearLayout>
<LinearLayout
android:id="@+id/page_3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="INVISIBLE">
<!-- Put stuff for page 3 -->
</LinearLayout>
<!-- ... -->
<LinearLayout
android:id="@+id/page_N"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="INVISIBLE">
<!-- Put stuff for page N -->
</LinearLayout>
</FrameLayout>
Basically how I would simulate the "page flipping effect" (perhaps with animation) is to make the current page invisible and make the new page visible. However, this implementation has some problems:
I was thinking if there's a way to dynamically load a page from an XML file during run time. As in, would it be possible to inflate a particular layout whenever the user demands it? Or if anyone has a better implementation that I haven't though of, please tell me!
Upvotes: 0
Views: 442
Reputation: 4387
Use a view flipper, for example
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ViewFlipper
android:id="@+id/viewflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/page_1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Put stuff for page 1 -->
</LinearLayout>
<LinearLayout
android:id="@+id/page_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="INVISIBLE">
<!-- Put stuff for page 2 -->
</LinearLayout>
<LinearLayout
android:id="@+id/page_3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="INVISIBLE">
<!-- Put stuff for page 3 -->
</LinearLayout>
<!-- ... -->
<LinearLayout
android:id="@+id/page_N"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="INVISIBLE">
<!-- Put stuff for page N -->
</LinearLayout>
</ViewFlipper>
</FrameLayout>
Now to flip between the different views, you would (in your java file)
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewflipper);
flipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.exit_slide_right_left));
flipper.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.enter_slide_right_left));
flipper.setDisplayedChild(n);
which will slide as if you're flipping forward, the animation file looks like:
(enter_slide_right_left.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="0%" android:toXDelta="-100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
and (exit_slide_right_left.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700" />
</set>
Upvotes: 1
Reputation: 2195
Maybe have each page as a separate resource? That way, you can just put all the pages in an array, and retrieve individual pages by index.
Upvotes: 0
Reputation: 29199
You can simply Use ViewFlipper for that purpose, and add views to flipper dynamically as user demand for that page.
Upvotes: 2