Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android自定義控件6--輪播圖廣告的實現,android6--

Android自定義控件6--輪播圖廣告的實現,android6--

編輯:關於android開發

Android自定義控件6--輪播圖廣告的實現,android6--


本文接著實現輪播圖廣告的監聽滾動

本文地址:http://www.cnblogs.com/wuyudong/p/5920757.html,轉載請注明源地址。

首先添加布局文件,實現小白點

shape_bg_point_enable.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <corners android:radius="5dp" />
    <solid android:color="@android:color/white" />
</shape>

shape_bg_point_disable.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <corners android:radius="5dp" />
    <solid android:color="@android:color/darker_gray" />

</shape>

再添加一個selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_enabled="true" android:drawable="@drawable/shape_bg_point_enable"></item>
    <item android:state_enabled="false" android:drawable="@drawable/shape_bg_point_disable"></item>

</selector>

將selector添加到視圖:

        for (int i = 0; i < imageResIds.length; i++) {
            imageView = new ImageView(this);
            imageView.setBackgroundResource(imageResIds[i]);
            imageViewList.add(imageView);

            // 加小白點,指示器
            pointView = new View(this);
            pointView.setBackgroundResource(R.drawable.selector_bg_point);
            layoutParams = new LayoutParams(7, 7);
            if (i != 0) {
                layoutParams.leftMargin = 10;
            }
            pointView.setEnabled(false);

            ll_point_container.addView(pointView, layoutParams);
        }

為ViewPager添加滑動監聽

public class MainActivity extends Activity implements OnPageChangeListener 
......
    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        // 滾動時調用
    }

    @Override
    public void onPageSelected(int position) {
        // 新條目被選中時調用
        position = position % 5;
        tv_desc.setText(contentDesc[position]);
        // 把之前的禁用,把最新的啟用
        ll_point_container.getChildAt(lastEnablePoint).setEnabled(false);
        ll_point_container.getChildAt(position).setEnabled(true);
        lastEnablePoint = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // 滾動狀態變化時調用

    }

開啟頁面自動輪詢:

        //開啟輪詢
        new Thread() {
            public void run() {
                while(isRunning) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //往下跳一位
                    runOnUiThread(new Runnable() {
                        
                        @Override
                        public void run() {
                            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                        }
                    });
                }
                
            };
            
            
        }.start();

完整代碼如下:

package com.wuyudong.viewpager;

import java.util.ArrayList;

import android.R.layout;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity implements OnPageChangeListener {

    private ViewPager viewPager;
    private int[] imageResIds;
    private ArrayList<ImageView> imageViewList;
    private ArrayList<View> pointViews;
    private LinearLayout ll_point_container;
    private String[] contentDesc;
    private TextView tv_desc;
    private int lastEnablePoint = 0;
    private boolean isRunning = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化布局 View 視圖
        initViews();

        // Model 數據
        initData();

        // Controller 控制器
        initAdapter();
        
        //開啟輪詢
        new Thread() {
            public void run() {
                while(isRunning) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //往下跳一位
                    runOnUiThread(new Runnable() {
                        
                        @Override
                        public void run() {
                            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                        }
                    });
                }
                
            };
            
            
        }.start();

    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        isRunning = false;
    }

    private void initViews() {
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setOnPageChangeListener(this);// 設置滾動更新

        tv_desc = (TextView) findViewById(R.id.tv_desc);

        ll_point_container = (LinearLayout) findViewById(R.id.ll_point_container);

    }

    private void initAdapter() {
        ll_point_container.getChildAt(0).setEnabled(true);
        tv_desc.setText(contentDesc[0]);
        // 設置適配器
        viewPager.setAdapter(new MyAdapter());
        
        //默認設置到中間的某個位置
        int pos = Integer.MAX_VALUE / 2;
        //viewPager.setCurrentItem(pos - 3);
        viewPager.setCurrentItem(5000000);
    }

    private void initData() {

        // 初始化要顯示的數據
        // 圖片資源id數組
        imageResIds = new int[] { R.drawable.a, R.drawable.b, R.drawable.c,
                R.drawable.d, R.drawable.e };
        // 文本描述
        contentDesc = new String[] { "鞏俐不低俗,我就不能低俗", "樸樹又回來啦!再唱經典老歌引萬人大合唱",
                "揭秘北京電影如何升級", "樂視網TV版大派送", "熱血屌絲的反殺" };
        imageViewList = new ArrayList<ImageView>();
        pointViews = new ArrayList<View>();
        ImageView imageView;
        View pointView;
        LayoutParams layoutParams;
        for (int i = 0; i < imageResIds.length; i++) {
            imageView = new ImageView(this);
            imageView.setBackgroundResource(imageResIds[i]);
            imageViewList.add(imageView);

            // 加小白點,指示器
            pointView = new View(this);
            pointView.setBackgroundResource(R.drawable.selector_bg_point);
            layoutParams = new LayoutParams(7, 7);
            if (i != 0) {
                layoutParams.leftMargin = 10;
            }
            pointView.setEnabled(false);

            ll_point_container.addView(pointView, layoutParams);
        }

    }

    class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        // 3、指定復用的判斷邏輯
        @Override
        public boolean isViewFromObject(View view, Object object) {
            // 當滑到新的條目,又返回來,view是否可以被復用
            return view == object;
        }

        // 1、返回要顯示的條目內容,創建條目
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // container:容器:ViewPager
            // position:當前要顯示的條目的位置
            position = position % 5;
            ImageView imageView = imageViewList.get(position);
            System.out.println("instantItem創建:" + position);
            // a、把view對象添加到container中
            container.addView(imageView);
            // b、把view對象返回給框架,適配器
            return imageView; // 必須要重寫,否則拋異常
        }

        // 2、銷毀條目
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // object 要銷毀的對象
            System.out.println("destroyItem銷毀:" + position);
            container.removeView((View) object);
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        // 滾動時調用
    }

    @Override
    public void onPageSelected(int position) {
        // 新條目被選中時調用
        position = position % 5;
        tv_desc.setText(contentDesc[position]);
        // 把之前的禁用,把最新的啟用
        ll_point_container.getChildAt(lastEnablePoint).setEnabled(false);
        ll_point_container.getChildAt(position).setEnabled(true);
        lastEnablePoint = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // 滾動狀態變化時調用

    }
}

 

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