Using Viewpager to achieve high imitation launcher drag effect _android

Source: Internet
Author: User
In front of a high imitation launcher and ink drag effect has been a lot of friends praise, the last article is mainly through the custom ViewGroup, a little trouble. Today, this class uses Viewpager to achieve the same effect, so that the code is less, but the effect is the same. Viewpager is a class that enables smooth switching of about two screens, which is provided by Google.

Using Viewpager first you need to introduce Android-support-v4.jar this jar package. The specific Viewpager usage, here does not introduce, oneself searches from the internet!
Let's look at the effect:


Effects please experience and the previous comparison. The following code:
First of all, the main.xml under layout.

Copy Code code as follows:

<?xmlversion= "1.0" encoding= "Utf-8"?>
<framelayoutxmlns: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/viewpager"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>
<relativelayout
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical" >
<linearlayout
Android:id= "@+id/viewgroup"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
Android:layout_marginbottom= "30DP"
Android:gravity= "Center_horizontal"
android:orientation= "Horizontal" >
</LinearLayout>
</RelativeLayout>
</FrameLayout>

Next, set the layout for each switch interface Item1.xml
Copy Code code as follows:

<?xmlversion= "1.0" encoding= "UTF-8"?>
<linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<imageview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:background= "@drawable/guide01" >
</ImageView>
</LinearLayout>

Some of the other interface layouts, like this one, are to modify the background image so that you don't repeat it,
Finally, the core code:
Copy Code code as follows:

Importjava.util.ArrayList;
importandroid.app.Activity;
Importandroid.os.Bundle;
importandroid.os.Parcelable;
Importandroid.support.v4.view.PagerAdapter;
Importandroid.support.v4.view.ViewPager;
Importandroid.support.v4.view.ViewPager.OnPageChangeListener;
Importandroid.view.LayoutInflater;
Importandroid.view.View;
Importandroid.view.ViewGroup;
Importandroid.view.ViewGroup.LayoutParams;
Importandroid.view.Window;
Importandroid.widget.ImageView;
publicclassmainactivityextendsactivity{
Viewpagerviewpager;
arraylist<view>list;
Viewgroupmain,group;
Imageviewimageview;
Imageview[]imageviews;
@Override
Publicvoidoncreate (bundlesavedinstancestate) {
Super.oncreate (savedinstancestate);
This.requestwindowfeature (Window.feature_no_title);
Layoutinflaterinflater=getlayoutinflater ();
List=newarraylist<view> ();
List.add (Inflater.inflate (r.layout.item1,null));
List.add (Inflater.inflate (r.layout.item2,null));
List.add (Inflater.inflate (r.layout.item3,null));
List.add (Inflater.inflate (r.layout.item4,null));
List.add (Inflater.inflate (r.layout.item5,null));
Imageviews=newimageview[list.size ()];
Viewgroupmain= (ViewGroup) inflater.inflate (r.layout.main,null);
Group is the linearlayout of R.layou.main in charge of wrapping small dots.
viewgroupgroup= (ViewGroup) Main.findviewbyid (R.id.viewgroup);
Viewpager= (Viewpager) Main.findviewbyid (R.id.viewpager);
For (Inti=0;i<list.size (); i++) {
Imageview=newimageview (Mainactivity.this);
Imageview.setlayoutparams (Newlayoutparams (10,10));
Imageview.setpadding (10,0,10,0);
Imageviews[i]=imageview;
if (i==0) {
The default entry program after the first picture is selected;
Imageviews[i].setbackgroundresource (R.drawable.guide_dot_white);
}else{
Imageviews[i].setbackgroundresource (R.drawable.guide_dot_black);
}
Group.addview (ImageView);
}
Setcontentview (main);
Viewpager.setadapter (Newmyadapter ());
Viewpager.setonpagechangelistener (Newmylistener ());
}
classmyadapterextendspageradapter{
@Override
Publicintgetcount () {
Returnlist.size ();
}
@Override
Publicbooleanisviewfromobject (VIEWARG0,OBJECTARG1) {
RETURNARG0==ARG1;
}
@Override
Publicintgetitemposition (Objectobject) {
Todoauto-generatedmethodstub
Returnsuper.getitemposition (object);
}
@Override
Publicvoiddestroyitem (VIEWARG0,INTARG1,OBJECTARG2) {
Todoauto-generatedmethodstub
((Viewpager) arg0). Removeview (List.get (arg1));
}
@Override
Publicobjectinstantiateitem (VIEWARG0,INTARG1) {
Todoauto-generatedmethodstub
((Viewpager) arg0). AddView (List.get (arg1));
Returnlist.get (ARG1);
}
@Override
Publicvoidrestorestate (PARCELABLEARG0,CLASSLOADERARG1) {
Todoauto-generatedmethodstub
}
@Override
Publicparcelablesavestate () {
Todoauto-generatedmethodstub
Returnnull;
}
@Override
Publicvoidstartupdate (Viewarg0) {
Todoauto-generatedmethodstub
}
@Override
Publicvoidfinishupdate (Viewarg0) {
Todoauto-generatedmethodstub
}
}
classmylistenerimplementsonpagechangelistener{
@Override
Publicvoidonpagescrollstatechanged (intarg0) {
Todoauto-generatedmethodstub
}
@Override
Publicvoidonpagescrolled (INTARG0,FLOATARG1,INTARG2) {
Todoauto-generatedmethodstub
}
@Override
Publicvoidonpageselected (intarg0) {
for (inti=0;i<imageviews.length;i++) {
IMAGEVIEWS[ARG0]
. Setbackgroundresource (R.drawable.guide_dot_white);
if (arg0!=i) {
Imageviews[i]
. Setbackgroundresource (R.drawable.guide_dot_black);
}
}
}
}
}

Finally, in a reminder, don't forget to join Android-support-v4.jar this jar package.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.