viewPager+Handler+Timer简单实现广告轮播效果

基本思想是在Avtivity中放一个ViewPager,然后通过监听去实现联动效果,代码理由详细的解释,我就不说了。

  MainActivity.java

 package com.example.administrator.imageviewlunbodemo;

 import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask; public class MainActivity extends Activity { private ViewPager myViewPager;
private List<View> myContiontar = new ArrayList<>(); //viewPager的数据源
private PagerAdapter myPagerAdapter; //有了数据源,必然要有适配器 private ImageView imageView1;
private ImageView imageView2;
private ImageView imageView3;
private ImageView imageView4;
private ImageView imageView5; private Timer mTimer;
private Timertask mTimertask; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initViews(); //初始化各种View myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) { } @Override
public void onPageSelected(int i) {
selectImageId(i);
} @Override
public void onPageScrollStateChanged(int i) { }
}); mTimertask = new Timertask();
mTimer = new Timer();
mTimer.schedule(mTimertask,0,2000);
// //启动时选择第一张图片
// imageView1.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_select); } //初始化各种View
private void initViews(){
// 先将xml文件 换成 view
myViewPager = (ViewPager) findViewById(R.id.viewpager); imageView1 = (ImageView) findViewById(R.id.first_fragment_down_image1);
imageView2 = (ImageView) findViewById(R.id.first_fragment_down_image2);
imageView3 = (ImageView) findViewById(R.id.first_fragment_down_image3);
imageView4 = (ImageView) findViewById(R.id.first_fragment_down_image4);
imageView5 = (ImageView) findViewById(R.id.first_fragment_down_image5); //建立五个view 去获得四个ImageView
View view1 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.lunbo_image1, null);
View view2 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.lunbo_image2,null);
View view3 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.lunbo_image3, null);
View view4 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.lunbo_image4, null);
View view5 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.lunbo_image5,null);
//加入到容器里面
myContiontar.add(view1);
myContiontar.add(view2);
myContiontar.add(view3);
myContiontar.add(view4);
myContiontar.add(view5);
//初始化 适配器
myPagerAdapter = new PagerAdapter() {
//返回显示多少项
@Override
public int getCount() {
return myContiontar.size();
} @Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
//滑动切换时,移除当前组件
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(myContiontar.get(position));
}
//没次滑动时生成的组件
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(myContiontar.get(position));
return myContiontar.get(position);
}
};
//设置适配器
myViewPager.setAdapter(myPagerAdapter);
} //选择那个图片
private void selectImageId(int i){
initImageBackGround();
switch (i){
case 0:
imageView1.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_select);
break;
case 1:
imageView2.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_select);
break;
case 2:
imageView3.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_select);
break;
case 3:
imageView4.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_select);
break;
case 4:
imageView5.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_select);
break;
}
}
//初始化 所有Image的背景
private void initImageBackGround(){
imageView1.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_normal);
imageView2.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_normal);
imageView3.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_normal);
imageView4.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_normal);
imageView5.setBackgroundResource(R.drawable.wallet_coin_purse_guide_purse_dot_normal);
} int count = 0;
private Handler mhandler = new Handler(){ @Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x111){
//操作
count++;
myViewPager.setCurrentItem(count % 5);
}
}
}; //建立一个Timertask
class Timertask extends TimerTask{ @Override
public void run() {
mhandler.sendEmptyMessage(0x111); //发送空消息
}
}
}

主布局文件里面放了一个ViewPager和五个ImageView(就是那种小点点)

activity_main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center|bottom">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:orientation="horizontal"
android:layout_gravity="center|bottom">
<ImageView
android:id="@+id/first_fragment_down_image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="@drawable/wallet_coin_purse_guide_purse_dot_normal"/>
<ImageView
android:id="@+id/first_fragment_down_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="@drawable/wallet_coin_purse_guide_purse_dot_normal"/>
<ImageView
android:id="@+id/first_fragment_down_image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="@drawable/wallet_coin_purse_guide_purse_dot_normal"/>
<ImageView
android:id="@+id/first_fragment_down_image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="@drawable/wallet_coin_purse_guide_purse_dot_normal"/>
<ImageView
android:id="@+id/first_fragment_down_image5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="@drawable/wallet_coin_purse_guide_purse_dot_normal"/>
</LinearLayout>
</FrameLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.5"
android:background="@color/fitst_fragment_image_color"/>
</LinearLayout>

在ViewPager中我创建了五个布局文件,都很简单,里面就只有一个ImageView去显示图片

images.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/first_fragment_lunbo_image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/image1"
/>
</LinearLayout>

然后在用Timer让它每隔2秒去发送一次消息,通知ViewPager更新,就形成了简单的图片轮播效果。

  效果图:

viewPager+Handler+Timer简单实现广告轮播效果

上一篇:Android使用ViewPager实现左右循环滑动及轮播效果


下一篇:jQuery 禁用select和取消禁用之disabled