Use ViewPager to implement high-imitation launcher drag between left and right

Source: Internet
Author: User

The previous article was highly praised by many friends for its high imitation launcher and ink drag effects. The previous article was mainly implemented through custom ViewGroup, which is a little troublesome. Today, the ViewPager class is used 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 between the left and right screens. It is provided by Google.

To use ViewPager, first introduce the android-support-v4.jar jar package. The usage of ViewPager is not described here. Search for it on the Internet!
Let's take a look at the effect first:


Compare the effect with the previous one. The code below:
The first is main. xml in layout.

Copy codeThe Code is 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 item1.xml for each switching interface.Copy codeThe Code is 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>

The layout of the other interfaces is the same as that of the background image,
The core code is as follows:Copy codeThe Code is 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 in R. layou. main that is responsible for 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 first image is selected after the program is entered by default;
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 the reminder, do not forget to add the android-support-v4.jar of 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.