Android如何实现图片轮播

实现图片导航界面的轮播,也可以用在app的引导图片实现

xml

  <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="fill_parent"
            android:layout_height="160dp" />

java

activity

viewPager = (ViewPager) mView.findViewById(R.id.viewPager);
        ImageView view1 = new ImageView(ctx);
        view1.setBackgroundResource(R.drawable.test1);
        mViewList.add(view1);
        ImageView view2 = new ImageView(ctx);
        view2.setBackgroundResource(R.drawable.test2);
        mViewList.add(view2);
        ImageView view3 = new ImageView(ctx);
        view3.setBackgroundResource(R.drawable.test3);
        mViewList.add(view3);
        //设置适配器
        adapter = new MhealthAdater(mViewList);
        viewPager.setAdapter(adapter);
        //默认显示的图片
        currentItem = (int) (Integer.MAX_VALUE * 0.5);
        viewPager.setCurrentItem((int) (Integer.MAX_VALUE * 0.5));
        //定时更新UI
        mhandler.sendEmptyMessageDelayed(0, 2000);
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                changeIcon(arg0);
                currentItem = arg0;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
            //处于滑动状态时不轮播图片
            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (arg0 == 0) {
                    isScroll = false;
                } else {
                    isScroll = true;
                }
            }
        });


    /*
     * 轮播图片
     */
    private Handler mhandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            //递归调用自身
            mhandler.sendEmptyMessageDelayed(0, 2000);
            if (isScroll) {
            } else {
                currentItem = currentItem + 1;
                viewPager.setCurrentItem(currentItem);
                changeIcon(currentItem);
            }

        }

    };

适配器


import java.util.List;

import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;


public class MhealthAdater extends PagerAdapter {
    private List mViewList; 
    public MhealthAdater(List mViewList){
        this.mViewList=mViewList;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
//      container.removeView(mViewList.get(position));
    }
//初始化
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        position %= mViewList.size();  
        ImageView view = mViewList.get(position);  
        //如果View已经在之前添加到了一个父组件,则必须先remove,否则会抛出IllegalStateException。  
        ViewParent vp =view.getParent();  
        if (vp!=null){  
            ViewGroup parent = (ViewGroup)vp;  
            parent.removeView(view);  
        }  
        container.addView(view);    
        //add listeners here if necessary  
        return mViewList.get(position);    
    }


    @Override
    public int getCount() {
        // 设置view数量为最大值,实现左右无限轮播
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
    //推荐使用,View是否来自对象
        return arg0==arg1;
    }
    }  

你可能感兴趣的:(Android)