在Android开发中,经常要用使用轮播图来展示广告或者其他信息,本文采用最少的代码实现最好的功能。使用本案例,可以十分方便的改变轮播图片的张数和内容。其功能如下:
1、定时轮播图片;
2、手动滑动图片;
3、导航按钮同步;
一、工程结构:

二、java代码如下:
package com.lyz.viewpage.activity;import java.util.ArrayList;import java.util.List;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;/** * 图片轮播 * */public class MainActivity extends Activity { PRivate ViewPager mViewPaper; private List<ImageView> images; private List<View> dots; private int currentItem; private int oldPosition = 0; private int[] imageIds = new int[]{ R.drawable.ic_func_adv_1, R.drawable.ic_func_adv_2, R.drawable.ic_func_adv_3 }; private TextView title; private ViewPagerAdapter adapter; private ScheduledExecutorService scheduledExecutorService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewPaper = (ViewPager) findViewById(R.id.vp); images = new ArrayList<ImageView>(); for(int i = 0; i < imageIds.length; i++){ ImageView imageView = new ImageView(this); imageView.setBackgroundResource(imageIds[i]); images.add(imageView); } dots = new ArrayList<View>(); dots.add(findViewById(R.id.dot_0)); dots.add(findViewById(R.id.dot_1)); dots.add(findViewById(R.id.dot_2)); adapter = new ViewPagerAdapter(); mViewPaper.setAdapter(adapter); mViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { dots.get(position).setBackgroundResource(R.drawable.dot_focused); dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal); oldPosition = position; currentItem = position; } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } private class ViewPagerAdapter extends PagerAdapter{ @Override public int getCount() { return images.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup view, int position, Object object) { view.removeView(images.get(position)); } @Override public Object instantiateItem(ViewGroup view, int position) { view.addView(images.get(position)); return images.get(position); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override protected void onStart() { super.onStart(); scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); scheduledExecutorService.scheduleWithFixedDelay( new ViewPageTask(), 2, 2, TimeUnit.SECONDS); } private class ViewPageTask implements Runnable{ @Override public void run() { currentItem = (currentItem + 1) % imageIds.length; mHandler.sendEmptyMessage(0); } } private Handler mHandler = new Handler(){ public void handleMessage(android.os.Message msg) { mViewPaper.setCurrentItem(currentItem); }; }; @Override protected void onStop() { super.onStop(); }}代码说明:该代码在使用的时候,只需要改变上述private int[] imageIds = new int[]{};里面的代码,增加或者减少参与轮播的图片的张数即可。如果要改变里面的图片,请把文件命名为drawable文件夹里面的图片名,采用直接替换即可。十分方便。三、xml文件代码:
<RelativeLayout 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:background="#ffffff" > <!-图片展示区域--> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="140dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="35dip" android:layout_alignBottom="@+id/vp" android:layout_alignParentLeft="true" android:background="#f0f0f0" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <View android:id="@+id/dot_0" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_focused" /> <View android:id="@+id/dot_1" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_normal" /> <View android:id="@+id/dot_2" android:layout_width="5dip" android:layout_height="5dip" android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/dot_normal" /> </LinearLayout> </LinearLayout></RelativeLayout>四、效果图如下:

源代码:链接:http://pan.baidu.com/s/1o7N9jwY 密码:a0gm
新闻热点
疑难解答