Android Tutorials Viewpager automatic loops and manual loops _android

Source: Internet
Author: User

Copy Code code as follows:

Package Com.imoyo.expert;

Import java.util.ArrayList;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
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.Window;
Import Android.view.ViewGroup.LayoutParams;
Import Android.widget.ImageView;

Public class Exactivity extends activity {
 viewpager Viewpager;
 ArrayList<View> list;
 viewgroup Main, group;
 boolean Cunhuan = false;
 imageview ImageView;
 imageview[] imageviews;
 private static int c_id = 0;
 static int page_id = 1;
 private Runnable switchtask = new Runnable () {
  public void Run () {
   if ( Cunhuan) {
    viewpager.setcurrentitem (page_id);  
    page_ id++;
   }
   cunhuan=true;
&NBSP;&NBSP;&NBSP;LOG.E ("Is_run", cunhuan+ "");
   mhandler.postdelayed (Switchtask, 3000);

}
};
Handler Mhandler = new Handler ();
@Override
protected void Onresume () {
Super.onresume ();
Switchtask.run ();
}
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
This.requestwindowfeature (Window.feature_no_title);
GetView ();

Imageviews = new Imageview[list.size ()];
ViewGroup main = (ViewGroup) inflater.inflate (R.layout.activity_main,
NULL);

ViewGroup Group = (viewgroup) Main.findviewbyid (R.id.viewgroup);

Viewpager = (Viewpager) Main.findviewbyid (R.id.viewpager);

for (int i = 0; i < 5; i++) {
ImageView = new ImageView (this);
Imageview.setlayoutparams (New Layoutparams (12, 12));
Imageviews[i] = ImageView;
if (i = = 0) {
Imageviews[i].setbackgroundresource (R.drawable.img_blue_point);
} else {
Imageviews[i].setbackgroundresource (R.drawable.img_white_point);
}
Group.addview (ImageView);
}

Setcontentview (main);
Viewpageradapter adapter = new Viewpageradapter (this, list);
Viewpager.setadapter (adapter);
Viewpager.setonpagechangelistener (listener);
Viewpager.setcurrentitem (2);

}

public void GetView () {
Inflater = Getlayoutinflater ();
List = new arraylist<view> ();
List.add (inflater.inflate (R.LAYOUT.ITEM4, null));
List.add (inflater.inflate (R.LAYOUT.ITEM5, null));

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));

List.add (inflater.inflate (r.layout.item1, null));
List.add (inflater.inflate (r.layout.item2, null));
}

Onpagechangelistener listener = new Onpagechangelistener () {

@Override
public void onpageselected (int arg0) {

if (arg0 = = 0) {
Viewpager.setcurrentitem (5, false);
page_id=5;
else if (arg0 = 8) {
Viewpager.setcurrentitem (3, false);
page_id=3;
}else{
page_id=arg0;
}
LOG.E ("Arg", arg0 + "");

for (int i = 2; i < 7; i++) {
Imageviews[i-2]
. Setbackgroundresource (R.drawable.img_white_point);
}
Switch (arg0) {
Case 7:
Case 2:
Imageviews[0]
. Setbackgroundresource (R.drawable.img_blue_point);
Break
Case 3:
Case 8:
IMAGEVIEWS[1]
. Setbackgroundresource (R.drawable.img_blue_point);
Break
Case 4:
IMAGEVIEWS[2]
. Setbackgroundresource (R.drawable.img_blue_point);
Break
Case 0:
Case 5:
IMAGEVIEWS[3]
. Setbackgroundresource (R.drawable.img_blue_point);
Break
Case 1:
Case 6:
IMAGEVIEWS[4]
. Setbackgroundresource (R.drawable.img_blue_point);
Break
}

}

@Override
public void onpagescrolled (int arg0, float arg1, int arg2) {
}

@Override
public void onpagescrollstatechanged (int arg0) {

}
};
Private Layoutinflater Inflater;
}

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.