ViewPager implements boot page and viewpager guides

Source: Internet
Author: User

ViewPager implements boot page and viewpager guides
ViewPager implements boot pages

I will not talk much about it. I will talk to the Code directly.

Fry. Activity01

1 package fry; 2 3 import java. util. arrayList; 4 import java. util. collection; 5 import java. util. iterator; 6 import java. util. list; 7 import java. util. listIterator; 8 9 import com. example. viewPager. r; 10 11 import android. app. activity; 12 import android. OS. bundle; 13 import android. support. v4.view. viewPager; 14 import android. support. v4.view. viewPager. onPageChangeListener; 15 import android. view. view; 16 import android. view. view. onClickListener; 17 import android. view. viewGroup. layoutParams; 18 import android. widget. imageView; 19 import android. widget. linearLayout; 20 21 public class Activity01 extends Activity implements OnPageChangeListener, OnClickListener {22 private ViewPager viewPager; 23 private List <View> views; // stores the ImageView 24 private int [] imageID in ViewPager; // 25 private ImagePagerAdapter adapter; // adapter 26 private LinearLayout linearLayout; 27 private int currentPoint; // The subscript 28 @ Override 29 protected void onCreate (Bundle savedInstanceState) {30 // TODO Auto-generated method stub 31 super. onCreate (savedInstanceState); 32 setContentView (R. layout. activity01); 33 initData (); 34 initView (); 35 36} 37 // initialize view 38 void initView () {39 adapter = new ImagePagerAdapter (views ); 40 viewPager = (ViewPager) findViewById (R. id. viewPager); 41 viewPager. setAdapter (adapter); 42 linearLayout = (LinearLayout) findViewById (R. id. linearLayout); 43 initPoint (); 44 viewPager. setOnPageChangeListener (this); 45} 46 47 // initialization data 48 void initData () {49 // put all the image IDs in the imageID set 50 imageID = new int [] {R. drawable. guide1, R. drawable. guide2, R. drawable. guide3, R. drawable. guide4}; 51 views = new ArrayList <View> (); 52 // set the LayoutParams parameter 53 LayoutParams layoutParams = new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); 54 for (int I: imageID) {55 // not sure 1 56 ImageView imageView = new ImageView (this); 57 imageView. setImageResource (I); 58 imageView. setLayoutParams (layoutParams); 59 views. add (imageView); 60} 61 62 63} 64 65/* 66 * point 67 */68 private void initPoint () at the bottom of the initialization page () {69 ImageView [] imageView = new ImageView [imageID. length]; 70 for (int I = 0; I <linearLayout. getChildCount (); I ++) {71 imageView [I] = (ImageView) linearLayout. getChildAt (I); 72 imageView [I]. setImageResource (R. drawable. point_normal); 73 imageView [I]. setOnClickListener (this); 74 imageView [I]. setTag (I); 75} 76 currentPoint = 0; 77 imageView [currentPoint]. setImageResource (R. drawable. point_select); 78} 79/* 80 * set all the points at the bottom of the page to the unselected status 81 */82 private ImageView [] clearPoint () {83 ImageView [] imageView = new ImageView [imageID. length]; 84 for (int I = 0; I <linearLayout. getChildCount (); I ++) {85 imageView [I] = (ImageView) linearLayout. getChildAt (I); 86 imageView [I]. setImageResource (R. drawable. point_normal); 87} 88 return imageView; 89} 90 91 @ Override 92 public void onPageScrollStateChanged (int arg0) {93 // TODO Auto-generated method stub 94 95} 96 97 @ Override 98 public void onPageScrolled (int arg0, float arg1, int arg2) {99 // TODO Auto-generated method stub100 101} 102 103/* 104 * method executed when the page is selected 105 * (non-Javadoc) 106 * @ see android. support. v4.view. viewPager. onPageChangeListener # onPageSelected (int) 107 */108 @ Override109 public void onPageSelected (int position) {110 // TODO Auto-generated method stub111 ImageView [] imageView = clearPoint (); 112 imageView [position]. setImageResource (R. drawable. point_select); 113} 114 115 // small dot Click Event 116 @ Override117 public void onClick (View v) {118 // TODO Auto-generated method stub119 int I = (Integer) v. getTag (); 120 viewPager. setCurrentItem (I); 121} 122 123}

 

Fry. ImagePagerAdapter

1 package fry; 2 3 import java. util. arrayList; 4 import java. util. list; 5 6 import android. support. v4.view. pagerAdapter; 7 import android. support. v4.view. viewPager; 8 import android. view. view; 9 import android. view. viewGroup; 10 11 public class ImagePagerAdapter extends PagerAdapter {12 13 private List <View> views; 14 15 16 17 public ImagePagerAdapter (List <View> views) {18 this. views = views; 19} 20 21 @ Override22 public int getCount () {23 // TODO Auto-generated method stub24 return this. views. size (); 25} 26/** 27 * position: display the image position in viewPager 28 * put the corresponding image to the corresponding position 29 * instantiateItem and destroyItem are the corresponding 30 * is to create an item, destroy item31 */32 @ Override33 public Object instantiateItem (ViewGroup container, int position) {34 (ViewPager) container ). addView (views. get (position); 35 return views. get (position); 36} 37 38 @ Override39 public boolean isViewFromObject (View arg0, Object arg1) {40 // TODO Auto-generated method stub41 return arg0 = arg1; 42} 43 44 @ Override45 public void destroyItem (ViewGroup container, int position, Object object) {46 (ViewPager) container ). removeView (views. get (position); 47} 48 49}

/ViewPager/res/layout/activity01.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7     <android.support.v4.view.ViewPager 8         android:id="@+id/viewPager" 9         android:layout_width="match_parent"10         android:layout_height="match_parent"11         >12         13     </android.support.v4.view.ViewPager>14     15     <LinearLayout 16         android:id="@+id/linearLayout"17         android:layout_width="wrap_content"18         android:layout_height="wrap_content"19         android:orientation="horizontal"20         android:layout_gravity="bottom|center"21         >22         <ImageView 23             android:id="@+id/point1"24             android:layout_width="wrap_content"25             android:layout_height="wrap_content"26             android:src="@drawable/point_select"27             android:padding="25dip"28             />29         <ImageView 30             android:id="@+id/point2"31             android:layout_width="wrap_content"32             android:layout_height="wrap_content"33             android:src="@drawable/point_normal"34             android:padding="25dip"35             />36         <ImageView 37             android:id="@+id/point3"38             android:layout_width="wrap_content"39             android:layout_height="wrap_content"40             android:src="@drawable/point_normal"41             android:padding="25dip"42             />43         <ImageView 44             android:id="@+id/point4"45             android:layout_width="wrap_content"46             android:layout_height="wrap_content"47             android:src="@drawable/point_normal"48             android:padding="25dip"49             />50         51     </LinearLayout>52 53 </FrameLayout>

 

 

1. Multiple controls (small dots here) can be accessed by using List

2. Dynamic controls can be used to set tags to differentiate controls.

Related Article

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.