Use ViewPager to achieve left-right circular sliding and sliding jump

Source: Internet
Author: User

In the previous article, ViewPager was used to achieve the high-imitation launcher drag effect. Later, many friends asked if they could achieve the circular sliding between left and right and the guide page. Today, the slide between the left and right is realized. As for the slide jump on the last page, this is also done, but the effect is not very good. I also hope that some friends who have implemented it can share it with me. Add an image on the last page and click jump. In this case, you can easily add an image by yourself. Click Next and jump to the page.

This article makes some minor modifications based on the implementation of the High-imitation launcher drag effect using ViewPager. You can refer to the previous section. Let's talk about the code!

First, let's look at the xml in layout.Copy codeThe Code is as follows: <? 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"
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>

And the previous page uses viewpager. To use ViewPager, first introduce the android-support-v4.jar jar package. Do not forget to add it yourself.
The following is the core code:Copy codeThe Code is as follows: package cn.com. karl. viewpager;
Import java. util. ArrayList;
Import android. app. Activity;
Import android. content. Intent;
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. Log;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. view. ViewGroup. LayoutParams;
Import android. view. Window;
Import android. widget. ImageView;
Public class MainActivity extends Activity {
ViewPager viewPager;
ArrayList <View> list;
ViewGroup main, group;
ImageView imageView;
ImageView [] imageViews;
Private static int c_id = 0;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
This. requestWindowFeature (Window. FEATURE_NO_TITLE );
LayoutInflater inflater = getLayoutInflater ();
List = new ArrayList <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 = new ImageView [list. size ()];
ViewGroup main = (ViewGroup) inflater. inflate (R. layout. main, null );

ViewGroup group = (ViewGroup) main. findViewById (R. id. viewGroup );
ViewPager = (ViewPager) main. findViewById (R. id. viewPager );
For (int I = 0; I <list. size (); I ++ ){
ImageView = new ImageView (MainActivity. this );
ImageView. setLayoutParams (new LayoutParams (12, 12 ));
// ImageView. setPadding (10, 0, 10, 0 );
ImageViews [I] = imageView;
If (I = 0 ){

ImageViews [I]. setBackgroundResource (R. drawable. guide_dot_white );
} Else {
ImageViews [I]. setBackgroundResource (R. drawable. guide_dot_black );
}
Group. addView (imageView );
}
SetContentView (main );
ViewPager. setAdapter (new MyAdapter ());
ViewPager. setOnPageChangeListener (new MyListener ());
ViewPager. setCurrentItem (300 );

}
Class MyAdapter extends PagerAdapter {
@ Override
Public int getCount (){
Return Integer. MAX_VALUE;
}
@ Override
Public boolean isViewFromObject (View arg0, Object arg1 ){
Return arg0 = arg1;
}
@ Override
Public int getItemPosition (Object object ){
// TODO Auto-generated method stub
Return super. getItemPosition (object );
}
@ Override
Public void destroyItem (View arg0, int arg1, Object arg2 ){
// TODO Auto-generated method stub
// (ViewPager) arg0). removeView (list. get (arg1 ));
}
@ Override
Public Object instantiateItem (View arg0, int arg1 ){
// TODO Auto-generated method stub
Try {
(ViewPager) arg0). addView (list. get (arg1% list. size (), 0 );
} Catch (Exception e ){
// TODO: handle exception
}
Return list. get (arg1% list. size ());
}
@ Override
Public void restoreState (Parcelable arg0, ClassLoader arg1 ){
// TODO Auto-generated method stub
}
@ Override
Public Parcelable saveState (){
// TODO Auto-generated method stub
Return null;
}
@ Override
Public void startUpdate (View arg0 ){
// TODO Auto-generated method stub
}
@ Override
Public void finishUpdate (View arg0 ){
// TODO Auto-generated method stub
}
}
Class MyListener implements OnPageChangeListener {
// Called when the sliding status changes
@ Override
Public void onPageScrollStateChanged (int arg0 ){
// TODO Auto-generated method stub
// Arg0 = arg0 % list. size ();

}
// Called when the current page is slide
@ Override
Public void onPageScrolled (int arg0, float arg1, int arg2 ){
// TODO Auto-generated method stub

}
// Called when a new page is selected
@ Override
Public void onPageSelected (int arg0 ){
If (arg0> 2 ){
Arg0 = arg0 % list. size ();
}
C_id = arg0;
For (int I = 0; I <imageViews. length; I ++ ){
ImageViews [arg0]
. SetBackgroundResource (R. drawable. guide_dot_white );
If (arg0! = I ){
ImageViews [I]
. SetBackgroundResource (R. drawable. guide_dot_black );
}
}

Log. e ("-------------", "current is the" + c_id + "page ");
}
}
}

The code is similar to the previous one. Let's study it on your own! See the following results:

Forget, this effect is not easy to use for image demonstration. Let's demonstrate it by yourself! Slide to the last one, and then slide to the first page. Let's take a look at the print under my logcat:

The default page is 0th.

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.