============ Problem Description ============
Main.xml Code:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:umadsdk= "Http://schemas.android.com/apk/res/com.LoveBus"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<linearlayout
Android:id= "@+id/linearlayout1"
Android:layout_width= "Fill_parent"
android:layout_height= "100.0dip"
android:background= "#FFFFFF" >
<textview
Android:id= "@+id/text1"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:layout_weight= "1.0"
android:gravity= "Center"
android:text= "page Card 1"
Android:textcolor= "#000000"
Android:textsize= "22.0dip"/>
<textview
Android:id= "@+id/text2"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:layout_weight= "1.0"
android:gravity= "Center"
android:text= "page Card 2"
Android:textcolor= "#000000"
Android:textsize= "22.0dip"/>
<textview
Android:id= "@+id/text3"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:layout_weight= "1.0"
android:gravity= "Center"
android:text= "page Card 3"
Android:textcolor= "#000000"
Android:textsize= "22.0dip"/>
</LinearLayout>
<imageview
Android:id= "@+id/cursor"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:scaletype= "Matrix"
Android:src= "@drawable/A"/>
<android.support.v4.view.viewpager
Android:id= "@+id/vpager"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
Android:layout_weight= "1.0"
Android:background= "#000000"
Android:flipinterval= "30"
android:persistentdrawingcache= "Animation"/>
</LinearLayout>
Lay1.xml Code:
<?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"
android:orientation= "Vertical"
android:background= "#158684" >
<button android:id= "@+id/lay1_button"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "lay1 button"/>
</LinearLayout>
Lay2.xml code is similar to Lay3.xml code and LAY1 code
Activity code:
Package Com.demo;
Import java.util.ArrayList;
Import java.util.List;
Import android.app.Activity;
Import Android.content.Context;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Matrix;
Import Android.os.Bundle;
Import android.os.Parcelable;
Import Android.support.v4.view.PagerAdapter;
Import Android.support.v4.view.ViewPager;
Import Android.support.v4.view.ViewPager.OnPageChangeListener;
Import Android.util.DisplayMetrics;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import android.view.animation.Animation;
Import android.view.animation.TranslateAnimation;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.TextView;
/**
* tab page gesture swipe toggle and animation effect
*
* @author D.winter
*
*/
public class Mainactivity extends Activity {
Viewpager is a class of an add-on package that comes with the Google SDK, and can be used for switching between screens.
Android-support-v4.jar
Private Viewpager mpager;//Page card content
Private list<view> listviews; tab page List
Private ImageView cursor;//Animated pictures
Private TextView T1, T2, t3;//page Header
private int offset = 0;//animated picture offset
private int currindex = 0;//Current Page card number
private int basenum = 3000;
private int nowbasenum = 3000;
private int bmpw;//animated picture width
Private Layoutinflater Inflater;
Private View View1, View2, VIEW3;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Initlayout ();
Initimageview ();
Inittextview ();
Initviewpager ();
}
/**
* Initialization of headers
*/
private void Inittextview () {
T1 = (TextView) Findviewbyid (R.ID.TEXT1);
T2 = (TextView) Findviewbyid (R.ID.TEXT2);
T3 = (TextView) Findviewbyid (R.ID.TEXT3);
T1.setonclicklistener (new Myonclicklistener (0));
T2.setonclicklistener (New Myonclicklistener (1));
T3.setonclicklistener (New Myonclicklistener (2));
}
/**
* Initialize Viewpager
*/
private void Initviewpager () {
Mpager = (Viewpager) Findviewbyid (R.id.vpager);
ListViews = new arraylist<view> ();
Layoutinflater minflater = Getlayoutinflater ();
Listviews.add (View1);
Listviews.add (VIEW2);
Listviews.add (VIEW3);
Mpager.setadapter (New Mypageradapter (listviews));
Mpager.setcurrentitem (Basenum);
Mpager.setonpagechangelistener (New Myonpagechangelistener ());
}
private void Initlayout () {
Inflater = (Layoutinflater) This
. Getsystemservice (Context.layout_inflater_service);
Layout of the first page
View1 = inflater.inflate (R.layout.lay1, NULL, FALSE);
Button button = (button) View1.findviewbyid (R.id.lay1_button);
Button.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
LOG.D ("Mxt", "Click on the Lay1 button");
}
});
Layout of the second page
View2 = inflater.inflate (R.layout.lay2, NULL, FALSE);
Button button2 = (button) View2.findviewbyid (R.id.lay2_button);
Button2.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
LOG.D ("Mxt", "Click on the Lay2 button");
}
});
The layout of a third page
VIEW3 = inflater.inflate (R.layout.lay3, NULL, FALSE);
Button Button3 = (button) View3.findviewbyid (R.id.lay3_button);
Button3.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
LOG.D ("Mxt", "Click on the Lay3 button");
}
});
}
/**
* Initialize Animation
*/
private void Initimageview () {
cursor = (ImageView) Findviewbyid (r.id.cursor);//The horizontal bar below the page card
BMPW = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.A)
. getwidth ();//Get Picture width
Displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
int screenw = dm.widthpixels;//Get resolution width
Offset = (SCREENW/3-bmpw)/2;//calculated offsets
Matrix matrix = new Matrix ();
Matrix.posttranslate (offset, 0);
Cursor.setimagematrix (matrix);//Set the initial position of the animation
}
/**
* Viewpager Adapter
*/
public class Mypageradapter extends Pageradapter {
Public list<view> mlistviews;
Public Mypageradapter (list<view> mlistviews) {
This.mlistviews = Mlistviews;
}
@Override
public void Destroyitem (View arg0, int arg1, Object arg2) {
LOG.I ("POS", "executed Destroyitem");
if (arg1 >= mlistviews.size ()-1) {
int newposition = Arg1%mlistviews.size ();
Arg1 = newposition;
((Viewpager) arg0). Removeview (Mlistviews.get (arg1));
// }
}
@Override
public void Finishupdate (View arg0) {
}
@Override
public int GetCount () {
return Mlistviews.size ();
return integer.max_value;
}
@Override
Public Object Instantiateitem (View arg0, int arg1) {
LOG.I ("POS", "executed Instantiateitem");
if (arg1 >= mlistviews.size ()-1) {
int newposition = arg1% mlistviews.size ();
Arg1 = newposition;
}
try {
((Viewpager) arg0). AddView (Mlistviews.get (arg1), 0);
} catch (Exception e) {
E.printstacktrace ();
}
Return Mlistviews.get (ARG1);
}
Public Object Instantiateitem (View container, int position) {
TODO auto-generated Method Stub
return Super.instantiateitem (container, position);
// }
@Override
public boolean isviewfromobject (View arg0, Object arg1) {
return arg0 = = (arg1);
}
@Override
public void Restorestate (parcelable arg0, ClassLoader arg1) {
}
@Override
Public parcelable saveState () {
return null;
}
@Override
public void Startupdate (View arg0) {
}
}
/**
* Head punctuation Tap Monitoring
*/
public class Myonclicklistener implements View.onclicklistener {
private int index = 0;
Public Myonclicklistener (int i) {
index = i;
}
@Override
public void OnClick (View v) {
if (currindex! = index) {
Switch (index) {
Case 0:
if (Currindex = = 1) {
Mpager.setcurrentitem (nowbaseNum-1);
} else if (Currindex = = 2) {
Mpager.setcurrentitem (Nowbasenum + 1);
}
Break
Case 1:
if (Currindex = = 0) {
Mpager.setcurrentitem (Nowbasenum + 1);
} else if (Currindex = = 2) {
Mpager.setcurrentitem (nowbaseNum-1);
}
Break
Case 2:
if (Currindex = = 1) {
Mpager.setcurrentitem (Nowbasenum + 1);
} else if (Currindex = = 0) {
Mpager.setcurrentitem (nowbaseNum-1);
}
Break
Default
Break
}
}
}
};
/**
* Page Card Toggle Monitoring
*/
public class Myonpagechangelistener implements Onpagechangelistener {
int one = offset * 2 + bmpw;//page Card 1-page Card 2 offset
INT-One * 2;//page Card 1-page Card 3 offset
@Override
public void onpageselected (int arg0) {
Animation Animation = null;
int pos = arg0% listviews.size ();
Nowbasenum = arg0;
Switch (POS) {
Case 0:
if (Currindex = = 1) {
Animation = new Translateanimation (one, 0, 0, 0);
} else if (Currindex = = 2) {
Animation = new Translateanimation (0, 0, 0);
}
Break
Case 1:
if (Currindex = = 0) {
Animation = new Translateanimation (offset, one, 0, 0);
} else if (Currindex = = 2) {
Animation = new Translateanimation (both, one, 0, 0);
}
Break
Case 2:
if (Currindex = = 0) {
Animation = new Translateanimation (offset, 0, 0);
} else if (Currindex = = 1) {
Animation = new Translateanimation (one, 0, 0);
}
Break
}
Currindex = pos;
Animation.setfillafter (TRUE);//true: The picture stops at the end of the animation
Animation.setduration (300);
Cursor.startanimation (animation);
}
@Override
public void onpagescrolled (int arg0, float arg1, int arg2) {
}
@Override
public void onpagescrollstatechanged (int arg0) {
}
}
}
============ Solution 1============
Code too many don't want to see. help you to top it.
Android Novice about the problem of sliding left and right, layout put <android.support.v4.view.ViewPager/><ImageView/> on the top of it.