Android uses ViewPager to implement sliding Ad Board instance source code

Source: Internet
Author: User

• Android-support-v4.jar, a software package officially provided to us by Google that is compatible with Android devices of lower versions and contains APIs that can only be used in Android3.0 and later versions. ViewPager is one of them. With ViewPager, we can do many things, from the simplest navigation to page switching menus.

• ViewPager allows the view to slide, just as the Lanucher slides left and right.
• This Demo shows you how to use ViewPager. When you do not slide the View, the system automatically switches to the next View every 5s (Cyclic switching ), when a user has a Touch to View, the 5s automatic switch function is canceled. There is an indicator to indicate which View is currently in.

Below is the running:

This is a very common application
The following code is provided:
1. Source FileCopy codeThe Code is as follows: package com. eyeandroid. viewpager;
Import java. util. ArrayList;
Import java. util. List;
Import java. util. concurrent. atomic. AtomicInteger;
Import android. annotation. SuppressLint;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
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. view. MotionEvent;
Import android. view. View;
Import android. view. View. OnTouchListener;
Import android. view. ViewGroup;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. ImageView;
@ SuppressLint ("HandlerLeak ")
Public class MainActivity extends Activity {
Private ImageView [] imageViews = null;
Private ImageView imageView = null;
Private ViewPager advPager = null;
Private AtomicInteger what = new AtomicInteger (0 );
Private boolean isContinue = true;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
InitViewPager ();
}

Private void initViewPager (){
AdvPager = (ViewPager) findViewById (R. id. adv_pager );
ViewGroup group = (ViewGroup) findViewById (R. id. viewGroup );
// Four background advertisements are stored here
List <View> advPics = new ArrayList <View> ();
ImageView img1 = new ImageView (this );
Img1.setBackgroundResource (R. drawable. advertising_default_1 );
AdvPics. add (img1 );
ImageView img2 = new ImageView (this );
Img2.setBackgroundResource (R. drawable. advertising_default_2 );
AdvPics. add (img2 );
ImageView img3 = new ImageView (this );
Img3.setBackgroundResource (R. drawable. advertising_default_3 );
AdvPics. add (img3 );
ImageView img4 = new ImageView (this );
Img4.setBackgroundResource (R. drawable. advertising_default );
AdvPics. add (img4 );

// Fill in imageviews
ImageViews = new ImageView [advPics. size ()];
// Small icon
For (int I = 0; I <advPics. size (); I ++ ){
ImageView = new ImageView (this );
ImageView. setLayoutParams (new LayoutParams (20, 20 ));
ImageView. setPadding (5, 5, 5, 5 );
ImageViews [I] = imageView;
If (I = 0 ){
ImageViews [I]
. SetBackgroundResource (R. drawable. banner_dian_focus );
} Else {
ImageViews [I]
. SetBackgroundResource (R. drawable. banner_dian_blur );
}
Group. addView (imageViews [I]);
}
AdvPager. setAdapter (new AdvAdapter (advPics ));
AdvPager. setOnPageChangeListener (new GuidePageChangeListener ());
AdvPager. setOnTouchListener (new OnTouchListener (){

@ Override
Public boolean onTouch (View v, MotionEvent event ){
Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN:
Case MotionEvent. ACTION_MOVE:
IsContinue = false;
Break;
Case MotionEvent. ACTION_UP:
IsContinue = true;
Break;
Default:
IsContinue = true;
Break;
}
Return false;
}
});
New Thread (new Runnable (){
@ Override
Public void run (){
While (true ){
If (isContinue ){
ViewHandler. sendEmptyMessage (what. get ());
WhatOption ();
}
}
}
}). Start ();
}

Private void whatOption (){
What. incrementAndGet ();
If (what. get ()> imageViews. length-1 ){
What. getAndAdd (-4 );
}
Try {
Thread. sleep (5000 );
} Catch (InterruptedException e ){

}
}
Private final Handler viewHandler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
AdvPager. setCurrentItem (msg. what );
Super. handleMessage (msg );
}
};
Private final class GuidePageChangeListener implements OnPageChangeListener {
@ Override
Public void onPageScrollStateChanged (int arg0 ){
}
@ Override
Public void onPageScrolled (int arg0, float arg1, int arg2 ){
}
@ Override
Public void onPageSelected (int arg0 ){
What. getAndSet (arg0 );
For (int I = 0; I <imageViews. length; I ++ ){
ImageViews [arg0]
. SetBackgroundResource (R. drawable. banner_dian_focus );
If (arg0! = I ){
ImageViews [I]
. SetBackgroundResource (R. drawable. banner_dian_blur );
}
}
}
}
Private final class AdvAdapter extends PagerAdapter {
Private List <View> views = null;
Public AdvAdapter (List <View> views ){
This. views = views;
}
@ Override
Public void destroyItem (View arg0, int arg1, Object arg2 ){
(ViewPager) arg0). removeView (views. get (arg1 ));
}
@ Override
Public void finishUpdate (View arg0 ){
}
@ Override
Public int getCount (){
Return views. size ();
}
@ Override
Public Object instantiateItem (View arg0, int arg1 ){
(ViewPager) arg0). addView (views. get (arg1), 0 );
Return views. get (arg1 );
}
@ Override
Public boolean isViewFromObject (View arg0, Object arg1 ){
Return arg0 = arg1;
}
@ Override
Public void restoreState (Parcelable arg0, ClassLoader arg1 ){
}
@ Override
Public Parcelable saveState (){
Return null;
}
@ Override
Public void startUpdate (View arg0 ){
}
}
}

2. layout File
This file plays a vital role in displaying images on the screen.Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<RelativeLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: orientation = "vertical">
<! -- ViewPager is a control -->
<Android. support. v4.view. ViewPager
Android: id = "@ + id/adv_pager"
Android: layout_width = "fill_parent"
Android: layout_height = "160dp">
</Android. support. v4.view. ViewPager>
<LinearLayout
Android: id = "@ + id/viewGroup"
Android: layout_below = "@ id/adv_pager"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "-25px"
Android: gravity = "right"
Android: orientation = "horizontal">
</LinearLayout>
</RelativeLayout>
</LinearLayout>

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.