Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android:用ViewPager實現廣告欄

Android:用ViewPager實現廣告欄

編輯:關於Android編程

現在常見的廣告欄效果,自動切換廣告,也可手動滑動切換。

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316092601.jpg

 

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316092684.jpg

我用ViewPager實現的,廢話不多說,上代碼:

 

1、布局文件 layout_ad_pager.xml

 

2、主要代碼 AdPagerMainActivity.java

 

 

package app.adpager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class AdPagerMainActivity extends ActionBarActivity {
    
    private ImageView mDefaultImage = null;
    
    private ViewPager mImagePager = null;
    
    private ImageView[] mImageViews = null;
    
    private AtomicInteger what = new AtomicInteger(0);
    
    private boolean isContinue = true;
    
    private List advPics;
    
    private Context mContext;
    
    private ImageView mImageView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_ad_pager);
        init();
        initView();
        initViewPager();
    }
    
    /**
     * 初始化數據
     */
    private void init() {
        mContext = this;
        advPics = new ArrayList();
    }
    
    /**
     * 初始化控件
     */
    private void initView() {
        // TODO Auto-generated method stub
        mDefaultImage = (ImageView) findViewById(R.id.home_default_image);
        mImagePager = (ViewPager) findViewById(R.id.adv_pager);
    }
    
    /**
     * void
     */
    private void initViewPager() {
        // TODO Auto-generated method stub
        mDefaultImage.setVisibility(View.GONE);
        mImagePager.setVisibility(View.VISIBLE);
        
        ImageView img1 = new ImageView(mContext);
        img1.setBackgroundResource(R.drawable.img1);
        advPics.add(img1);
        
        ImageView img2 = new ImageView(mContext);
        img2.setBackgroundResource(R.drawable.img2);
        advPics.add(img2);
        
        ImageView img3 = new ImageView(mContext);
        img3.setBackgroundResource(R.drawable.img3);
        advPics.add(img3);
        
        ImageView img4 = new ImageView(mContext);
        img4.setBackgroundResource(R.drawable.img4);
        advPics.add(img4);
        
        // group是R.layou.mainview中的負責包裹小圓點的LinearLayout.
        ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);
        mImageViews = new ImageView[advPics.size()];
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(13, 13);
        layoutParams.setMargins(5, 0, 5, 1);
        
        for (int i = 0; i < advPics.size(); i++) {
            mImageView = new ImageView(this);
            mImageView.setLayoutParams(layoutParams);
            mImageViews[i] = mImageView;
            if (i == 0) {
                // 默認選中第一張圖片
                mImageViews[i].setBackgroundResource(R.drawable.item_focus);
            }
            else {
                mImageViews[i].setBackgroundResource(R.drawable.item_unfocus);
            }
            group.addView(mImageViews[i]);
        }
        
        mImagePager.setAdapter(new AdvAdapter(advPics));
        mImagePager.setOnPageChangeListener(new GuidePageChangeListener());
        // 按下時不繼續定時滑動,彈起時繼續定時滑動
        mImagePager.setOnTouchListener(new View.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() > mImageViews.length - 1) {
            what.getAndAdd(-mImageViews.length);
        }
        try {
            if (what.get() == 1) {
                Thread.sleep(3000);
            }
            else {
                Thread.sleep(2000);
            }
        }
        catch (InterruptedException e) {
        }
    }
    
    /**
     * 處理定時切換廣告欄圖片的句柄
     */
    @SuppressLint(HandlerLeak)
    private final Handler viewHandler = new Handler() {
        
        @Override
        public void handleMessage(Message msg) {
            mImagePager.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) {
            arg0 = arg0 % advPics.size();
            for (int i = 0; i < mImageViews.length; i++) {
                mImageViews[arg0].setBackgroundResource(R.drawable.item_focus);
                if (arg0 != i) {
                    mImageViews[i].setBackgroundResource(R.drawable.item_unfocus);
                }
            }
            what.set(arg0);
        }
    }
    
    /**
     * @Description: 廣告欄PaperView 圖片適配器
     */
    private final class AdvAdapter extends PagerAdapter {
        private List views = null;
        
        public AdvAdapter(List 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) {
            
        }
    }
}

 

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316092646.png

 

 

 

 

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved