欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android利用ViewPager实现滑动广告板实例源码

程序员文章站 2023-11-04 14:49:46
•android-support-v4.jar,这是谷歌官方给我们提供的一个兼容低版本android设备的软件包,里面包囊了只有在android3.0以上可以使...
•android-support-v4.jar,这是谷歌官方给我们提供的一个兼容低版本android设备的软件包,里面包囊了只有在android3.0以上可以使用的api。而viewpager就是其中之一,利用它我们可以做很多事情,从最简单的导航,到页面切换菜单等等。

•viewpager的功能就是可以使视图滑动,就像lanucher左右滑动那样。
•本demo向大家演示viewpager的使用,并在用户未滑动view时,每隔5s钟自动切换到下一个view(循环切换),而当用户有touch到view时取消5s自动切换功能。并有指示器指示当前处于哪一个view。

下面是运行的截图:
Android利用ViewPager实现滑动广告板实例源码 
这个是非常常见的应用
下面给出实现的代码:
1.源文件
复制代码 代码如下:

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);
// 这里存放的是四张广告背景
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);

// 对imageviews进行填充
imageviews = new imageview[advpics.size()];
//小图标
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.布局文件
这个文件在屏幕上的图片显示具有至关重要的作用
复制代码 代码如下:

<?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是一个控件 -->
<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>