Android Development using Viewpager to achieve the picture left and right sliding switch effect _android

Source: Internet
Author: User
Tags object object

Android pictures around the switch can be seen everywhere, today I also try to look up data to try to do a bit, quite simple a small demo, but also found some problems, words not to say, on the code ~:

Using 3 XML files as Viewpager sliding page, the layout is the same, showing only one of the following:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" match_parent "
  android:o" rientation= "vertical"
  >
  <imageview
    android:layout_width= "match_parent"
    android:layout_ height= "Match_parent"
    android:scaletype= "Centercrop"
    android:src= "@mipmap/view1"/>
</ Linearlayout>

Just used a imageview as a container for displaying pictures.

Main Page layout:

<?xml version= "1.0" encoding= "UTF-8"?> <relativelayout android:orientation=
"vertical"
android: layout_height= "Match_parent"
android:layout_width= "match_parent"
xmlns:android= "http://" Schemas.android.com/apk/res/android ">
<!--switching card-->
<android.support.v4.view.viewpager
  android:layout_height= "Wrap_content"
  android:layout_width= "match_parent"
  android:id= "@+id/viewPager" ></android.support.v4.view.ViewPager>
<!--dot-->
<linearlayout
  android:o rientation= "Horizontal"
  android:layout_height= "wrap_content"
  android:layout_width= "Fill_parent"
  android:id= "@+id/viewgroup"
  android:layout_alignparentbottom= "true"
  android:gravity= "Center_ Horizontal "
  android:layout_marginbottom=" 40DP "></LinearLayout>
</RelativeLayout>

The next step is the code for the main function:

Package com.yztc.lx.homework;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.support.v4.view.PagerAdapter;
Import Android.support.v4.view.ViewPager;
Import Android.view.LayoutInflater;
Import Android.view.ViewGroup.LayoutParams;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ImageView;
Import java.util.ArrayList;
Import java.util.List;
 /** * Created by Lx on 2016/8/6.
  * * Public class Layout1activity extends activity {private Viewpager Viewpager;
  Private Pageradapter adapter;
  Private list<view> viewpages = new arraylist<> ();
  Parcel point of LinearLayout private viewgroup group;
  Private ImageView ImageView;
  Define a ImageView array to store the generated small garden point private imageview[] imageviews;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.LAYOUT.LAYOUT1);
    Initview ();
    Initpageadapter ();
    Initpointer ();
  Initevent (); }//Bind event for control, bind adapter Private void Initevent () {Viewpager.setadapter (adapter);
  Viewpager.addonpagechangelistener (New Guidepagechangelistener ()); }//Initialize Viewpager private void Initpageadapter () {/** * for these several page pages that you want to dynamically load, use Layoutinflater.inflate () to locate their layout files
    , and is instantiated as a View object/layoutinflater Inflater = Layoutinflater.from (this);
    View Page1 = inflater.inflate (r.layout.page1, NULL);
    View Page2 = inflater.inflate (r.layout.page2, NULL);
    View Page3 = inflater.inflate (R.layout.page3, NULL);
    Add to the collection Viewpages.add (Page1);
    Viewpages.add (Page2);
    Viewpages.add (Page3); adapter = new Pageradapter () {//Get current interface number @Override public int getcount () {return viewpages.siz
      E (); //Determine if the object generates the page @Override public boolean isviewfromobject (view view, Object object) {return vie
      W = = object; @Override public void Destroyitem (ViewGroup container, int position, object object) {Container.remo Veview (viEwpages.get (position));  //Returns an object that indicates which object the Pageradapter adapter chooses to place in the current Viewpager @Override public object Instantiateitem (viewgroup
        container, int position) {View view = Viewpages.get (position);
        Container.addview (view);
      return view;
  }
    };
    }//Bound control private void Initview () {Viewpager = (Viewpager) Findviewbyid (R.id.viewpager);
  Group = (viewgroup) Findviewbyid (R.id.viewgroup);
    }//Initialize the following dot method private void Initpointer () {///How many interfaces are new for how long array imageviews = new imageview[viewpages.size ()];
      for (int i = 0; i < imageviews.length i++) {ImageView = new ImageView (this);
      Sets the width-height imageview.setlayoutparams of the control (new Layoutparams (25, 25));
      Sets the padding property of the control imageview.setpadding (20, 0, 20, 0);
      Imageviews[i] = ImageView; Initializes the first page of the picture's original point to the selected state if (i = = 0) {//Represents the current picture Imageviews[i].setbackgroundresource (r.mipmap.page_i
        ndicator_focused);
     /**    * When dynamically generating ImageView in Java code * to set its Backgroundresource property is valid * Set Imageresource property Invalid/} E
      LSE {Imageviews[i].setbackgroundresource (r.mipmap.page_indicator_unfocused);
    } group.addview (Imageviews[i]); }//viewpager Onpagechangelistener Listener event, calling public class Guidepagechangelistener implements V when Viewpager page changes Iewpager.onpagechangelistener {@Override public void onpagescrolled (int position, float positionoffset, int posit Ionoffsetpixels) {}//page sliding completes @Override public void onpageselected (int position) {//Judge is currently on that page , the ImageView origin of the corresponding subscript is set to the selected state of the picture for (int i = 0; i < imageviews.length; i++) {imageviews[position].setbackg
        Roundresource (r.mipmap.page_indicator_focused);
        if (position!= i) {imageviews[i].setbackgroundresource (r.mipmap.page_indicator_unfocused); }}///monitor page status, 0--still 1--sliding 2--slide complete @Override public void Onpagescrollstatechanged (int state) {}}} 

In this way, a simple slide picture of the demo is complete, you need to note that: in the Java code to generate ImageView, if you want to set the displayed picture, to use Setbackgroundresource () Method rather than the Setimageresource () method

The above is a small set to introduce the Android development of the use of Viewpager to achieve a picture of the sliding switch effect, I hope to be helpful to everyone, if you have any questions welcome to my message, small series will promptly reply to everyone, and this is also very grateful to you for your support cloud Habitat community site!

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.