Android利用ViewPager實現滑動廣告板

來源:互聯網
上載者:User

•android-support-v4.jar,這是Google官方給我們提供的一個相容低版本Android裝置的軟體包,裡麵包囊了只有在Android3.0以上可以使用的api。而ViewPager就是其中之一,利用它我們可以做很多事情,從最簡單的導航,到頁面切換菜單等等。 •ViewPager的功能就是可以使視圖滑動,就像Lanucher左右滑動那樣。 •本Demo向大家示範ViewPager的使用,並在使用者未滑動View時,每隔5s鐘自動切換到下一個View(迴圈切換),而當使用者有Touch到View時取消5s自動切換功能。並有指標指示當前處於哪一個View。
下面是啟動並執行:


這個是非常常見的應用 下面給出實現的代碼: 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;@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void handleMessage(Message msg) {advPager.setCurrentItem(msg.what);super.handleMessage(msg);}};private final class GuidePageChangeListener implements OnPageChangeListener {@Overridepublic void onPageScrollStateChanged(int arg0) {}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic 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;}@Overridepublic void destroyItem(View arg0, int arg1, Object arg2) {((ViewPager) arg0).removeView(views.get(arg1));}@Overridepublic void finishUpdate(View arg0) {}@Overridepublic int getCount() {return views.size();}@Overridepublic Object instantiateItem(View arg0, int arg1) {((ViewPager) arg0).addView(views.get(arg1), 0);return views.get(arg1);}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}@Overridepublic void restoreState(Parcelable arg0, ClassLoader arg1) {}@Overridepublic Parcelable saveState() {return null;}@Overridepublic 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>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.