In Android, ViewPager is used to implement screen page switching and page carousel effects. androidviewpager

Source: Internet
Author: User

In Android, ViewPager is used to implement screen page switching and page carousel effects. androidviewpager

Previously, I wrote a blog post titled "using ViewFlipper in Android to implement screen switching" about how to implement screen page switching. ViewPager is more suitable for complex view switching than ViewFlipper, in addition, Viewpager has its own adapter, which makes it adapt to complex objects and enables dynamic data loading.

ViewPager is a software package officially provided by Google that is compatible with Android devices of lower versions. It contains APIs that can only be used in Android 3.0 and later versions. Viewpager is one of them. With it, we can do many things, from the simplest navigation to the page menu and so on.

The following shows two simple effects that ViewPager can achieve:

First: Page switching on the screen (similar to ViewFlipper)

The implementation result is as follows (4 Views (layout )):

Step 1: Add the layout file to the Master layout file.Viewpager_layout.xml

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" android:layout_width="match_parent" 4     android:layout_height="match_parent"> 5     <android.support.v4.view.ViewPager 6         android:layout_width="match_parent" 7         android:layout_height="match_parent" 8         android:layout_gravity="center" 9         android:gravity="center"10         android:id="@+id/vp">11         <android.support.v4.view.PagerTabStrip12             android:layout_width="match_parent"13             android:layout_height="wrap_content"14             android:id="@+id/tap">15         </android.support.v4.view.PagerTabStrip>16     </android.support.v4.view.ViewPager>17 </LinearLayout>

 

Note:

<1. ViewPager and PagerTabStrip both need to write the full package name. Otherwise, ClassNotFound

<2. In the API, PagerTabStrip is used as a sub-tag of ViewPager in layout xml and cannot be used out. Otherwise, an error is reported.

<3. You can use the attribute android: layout_gravity = TOP | BOTTOM in the PagerTabStrip label to specify the title position.

<4. to display the title of a page of PagerTabStrip, you must implement getPageTitle (int) in the adapter of ViewPager)

Step 2: Create a View File for display switching in layout (The example contains four layout1, 2/3, and 4.xml. here is a typical example ):
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     xmlns:app="http://schemas.android.com/apk/res-auto" 4     android:orientation="vertical" android:layout_width="match_parent" 5     android:layout_height="match_parent"> 6     <ImageView 7         android:layout_width="match_parent" 8         android:layout_height="match_parent" 9         android:src="@mipmap/a1"10         android:scaleType="centerCrop"11         android:id="@+id/iv1" />12 </LinearLayout>
Step 3: ViewPagerDemo. Java (the pagerTabStrip attribute is not set here ):

Instantiate the ViewPager component in the Activity and set its Adapter (that is, PagerAdapter, with the same method as ListView)

Implement a PagerAdapter and override the following methods:

InstantiateItem (ViewGroup, int) // used to instantiate a page card

DestroyItem (ViewGroup, int, Object) // delete a page card

GetCount () // number of return page cards

IsViewFromObject (View, Object) // checks whether two objects are equal

GetPageTitle (int position) // you can specify the title of a tag.

Set the attribute of the indicator label

PagerTabStrip. setTabIndicatorColor (); // indicator color

PagerTabStrip. setBackgroundColor (); // background color

PagerTabStrip. setTextColor (Color. WHITE); // font Color

1 import android. OS. bundle; 2 import android. support. annotation. nullable; 3 import android. support. v4.view. pagerAdapter; 4 import android. support. v4.view. viewPager; 5 import android. support. v7.app. appCompatActivity; 6 import android. view. view; 7 import android. view. viewGroup; 8 import java. util. arrayList; 9/** 10 * Created by panchengjia on 2016/12/1. 11 */12 public class ViewPagerDemo extends AppCompatActivity {13 private ViewPager vp; 14 // declare 15 ArrayList for storing the set of child views under ViewPager <View> views = new ArrayList <> (); 16 // Title of each view in the display effect 17 String [] titles = {"No. 1 beauty", "No. 2 beauty", "No. 3 beauty", "No. 4 beauty "}; 18 @ Override19 protected void onCreate (@ Nullable Bundle savedInstanceState) {20 super. onCreate (savedInstanceState); 21 setContentView (R. layout. viewpager_layout); 22 vp = (ViewPager) findViewById (R. id. vp); 23 initView (); // call the initialization view Method 24 vp. setAdapter (new MyAdapter (); // set adapter 25} 26 // Method for initializing the view (use the layout filler to get the view for sliding and store it in the corresponding set) 27 private void initView () {28 View v1 = getLayoutInflater (). inflate (R. layout. layout1, null); 29 View v2 = getLayoutInflater (). inflate (R. layout. layout2, null); 30 View v3 = getLayoutInflater (). inflate (R. layout. layout3, null); 31 View v4 = getLayoutInflater (). inflate (R. layout. layout4, null); 32 views. add (v1); 33 views. add (v2); 34 views. add (v3); 35 views. add (v4); 36} 37 class MyAdapter extends PagerAdapter {38 @ Override39 public int getCount () {40 return views. size (); 41} 42 @ Override43 public boolean isViewFromObject (View view, Object object) {44 return view = object; 45} 46 // rewrite and destroy the sliding view layout (remove the sub-view from the view storage set (ViewGroup) 47 @ Override48 public void destroyItem (ViewGroup container, int position, Object object) {49 container. removeView (views. get (position); 50} 51 // override initialize the sliding view layout (retrieve the corresponding view from the gallery and add it to the ViewGroup) 52 @ Override53 public Object instantiateItem (ViewGroup container, int position) {54 View v = views. get (position); 55 container. addView (v); 56 return v; 57} 58 @ Override59 public CharSequence getPageTitle (int position) {60 return titles [position]; 61} 62} 63}
Type 2: Page carousel effect view (implementation of the boot page when the program is started for the first time)

The implementation result is as follows (Sliding Guide for three views ):

Note before starting the code: 1. Loop carousel is not implemented this time; 2,Navigation origin resource image annotation: default_holo is not selected,Touched_holo is the solid state after selection. (You can also draw it by yourself using Shape)

 

Step 1: Add the layout file to the Master layout file.Viewpager_layout.xml

 

The main layout is FrameLayout. In ViewPager (because of the navigation origin, PagerTabStrip is not set here), three navigation origins are nested (determined by the number of sliding views) the linearLayout layout of (this setting is at the bottom ):

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout 3     xmlns:android="http://schemas.android.com/apk/res/android" 4     xmlns:tools="http://schemas.android.com/tools" 5     android:id="@+id/activity_main" 6     android:layout_width="match_parent" 7     android:layout_height="match_parent" 8     tools:context="com.example.administrator.myapplication11.MainActivity"> 9     <android.support.v4.view.ViewPager10         android:layout_width="match_parent"11         android:layout_height="match_parent"12         android:id="@+id/vp">13     </android.support.v4.view.ViewPager>14     <LinearLayout15         android:id="@+id/point_layout"16         android:layout_width="match_parent"17         android:layout_height="wrap_content"18         android:layout_gravity="bottom"19         android:orientation="horizontal">20         <ImageView21             android:layout_width="0dp"22             android:layout_height="wrap_content"23             android:layout_weight="1"24             android:src="@mipmap/default_holo"/>25         <ImageView26             android:layout_width="0dp"27             android:layout_height="wrap_content"28             android:layout_weight="1"29             android:src="@mipmap/default_holo" />30         <ImageView31             android:layout_width="0dp"32             android:layout_height="wrap_content"33             android:layout_weight="1"34             android:src="@mipmap/default_holo"35             android:id="@+id/imageView" />36     </LinearLayout>37 </FrameLayout>

 

Step 2: The view used for sliding switching in Layout (three layout1/2/3.xmlin the example, which is typical here) is the same as the first one.

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" android:layout_width="match_parent" 4     android:layout_height="match_parent"> 5     <ImageView 6         android:layout_width="match_parent" 7         android:layout_height="match_parent" 8         android:scaleType="centerCrop" 9         android:src="@mipmap/genie"/>10 </LinearLayout>

 

Step 3: MainActivity. Java

 

You can use the OnPageChangeListener interface to change the navigation origin status when switching views.) for details about the methods in OnPageChangeListener, refer to the blog post.

 

Detailed description of the setOnPageChangeListener method of ViewPagerI will not repeat it here:

 

1 import android. support. v4.view. pagerAdapter; 2 import android. support. v4.view. viewPager; 3 import android. support. v7.app. appCompatActivity; 4 import android. OS. bundle; 5 import android. view. view; 6 import android. view. viewGroup; 7 import android. widget. imageView; 8 import android. widget. linearLayout; 9 import java. util. arrayList; 10/** 11 * Created by panchengjia on 2016/11/30. 12 */13 public class MainActivity extends AppCompatActivity implements ViewPager. onPageChangeListener {14 private ViewPager vp; 15 private LinearLayout point_layout; 16 ArrayList <View> views = new ArrayList <> (); 17 // instantiate the imageView (navigation origin) set 18 ArrayList <ImageView> imageViews = new ArrayList <> (); 19 int currImage; // record the current page (navigation origin) 20 @ Override21 protected void onCreate (Bundle savedInstanceState) {22 super. onCreate (savedInstanceState); 23 setContentView (R. layout. activity_main); 24 vp = (ViewPager) findViewById (R. id. vp); 25 initView (); // call the initialization view Method 26 initPoint (); // call the Method 27 vp for initializing the navigation origin. addOnPageChangeListener (this); 28 vp. setAdapter (new MyAdapter (); 29} 30/* Add the imageView (navigation origin) contained in point_layout to 31 * in the imageViewS set and set layout1 (first view) the 32 * image of the navigation origin (corresponding to the set 0 subscript) is touched_holo (image in the touch state) 33 */34 private void initPoint () {35 point_layout = (LinearLayout) findViewById (R. id. point_layout); 36 int counnt = point_layout.getChildCount (); // obtain the number of point 37 for (int I = 0; I <counnt; I ++) {38 imageViews. add (ImageView) point_layout.getChildAt (I); 39} 40 imageViews. get (0 ). setImageResource (R. mipmap. touched_holo); 41} 42 private void initView () {43 View v1 = getLayoutInflater (). inflate (R. layout. layout1, null); 44 View v2 = getLayoutInflater (). inflate (R. layout. layout2, null); 45 View v3 = getLayoutInflater (). inflate (R. layout. layout3, null); 46 views. add (v1); 47 views. add (v2); 48 views. add (v3); 49} 50 // OnPageChangeListener method, which is not implemented here 51 @ Override52 public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {53 54} 55 // After sliding to the view at the corresponding position, different states of the navigation origin (image) 56 @ Override57 public void onPageSelected (int position) {58 ImageView preView = imageViews. get (currImage); 59 preView. setImageResource (R. mipmap. default_holo); 60 ImageView currView = imageViews. get (position); 61 currView. setImageResource (R. mipmap. touched_holo); 62 currImage = position; 63} 64 // The OnPageChangeListener method, which is not implemented here 65 @ Override66 public void onPageScrollStateChanged (int state) {67 68} 69 class MyAdapter extends PagerAdapter {70 @ Override71 public int getCount () {72 return views. size (); 73} 74 @ Override75 public boolean isViewFromObject (View, Object object) {76 return view = Object; 77} 78 @ Override79 public void destroyItem (ViewGroup container, int position, Object object) {80 container. removeView (views. get (position); 81} 82 @ Override83 public Object instantiateItem (ViewGroup container, int position) {84 View v = views. get (position); 85 container. addView (v); 86 return v; 87} 88} 89}

 

So far, the Code required to implement the example of ViewPager has been completed. Of course, this is only the simplest function of ViewPager, and some advanced ViewPager usage will be updated in the future. Welcome to continue to support it.

Of course, the length is limited. This is only the simplest function of ViewPager,
Author: Go to Pikachu
Link: http://www.imooc.com/article/2580
Source: Of course, the length of MOOC is limited. This is only the simplest function of ViewPager,
Author: Go to Pikachu
Link: http://www.imooc.com/article/2580
Source: MOOC, of course, this is only the simplest function of ViewPager. Some advanced ViewPager usage will be updated in the future. You are welcome to continue to support it.

 

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.