使用Handler中postDelayed方法实现广告页面无限循环轮播效果

效果展示:
使用Handler中postDelayed方法实现广告页面无限循环轮播效果_第1张图片

直接上代码:
1.MainActivity的代码:

public class MainActivity extends Activity
{
    private ViewPager viewPager;

    private ImageView[] imageViews;

    private int COUNT = 4;

    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        fillData();
        initViewpager();
        mHandler=new Handler();
    }

    private void startLoop(final int delay){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
                startLoop(delay);
            }
        },delay);
    }

    private void fillData()
    {
        imageViews = new ImageView[COUNT];
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(R.mipmap.car1);
        imageViews[0] = imageView;

        imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(R.mipmap.car2);
        imageViews[1] = imageView;

        imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(R.mipmap.car3);
        imageViews[2] = imageView;

        imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(R.mipmap.car4);
        imageViews[3] = imageView;
    }

    // 设置Adapter
    private void initViewpager()
    {
        LoopViewpagerAdapter homepager = new LoopViewpagerAdapter(this,
                imageViews);
        viewPager.setAdapter(homepager);
        viewPager.setCurrentItem(1000 / 2
                - (1000 / 2 % COUNT));
    }

    public void doClick(View view){
        if(view.getId()==R.id.btn_start){
            startLoop(1000);
        }else{
            mHandler.removeMessages(0);
        }
    }

}

2.适配器的代码:

public class LoopViewpagerAdapter extends PagerAdapter
{
    private ImageView[] imageViews;
    private Context mContext;

    public LoopViewpagerAdapter(Context context, ImageView[] images)
    {
        this.imageViews = images;
        this.mContext = context;
    }
    @Override
    public int getCount()
    {
        return 1000;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1)
    {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object)
    {
        container.removeView(imageViews[position % imageViews.length]);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        View iv = imageViews[position % imageViews.length];
        // if (container != null)
        // {
        // container.removeView(iv);
        // }
        container.addView(iv);
        return iv;
    }

}

3.xml布局中的代码:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="250dp" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="match_parent"
        android:onClick="doClick"
        android:layout_height="wrap_content"
        android:text="开始"/>
    <Button
        android:id="@+id/btn_stop"
        android:layout_width="match_parent"
        android:onClick="doClick"
        android:layout_height="wrap_content"
        android:text="结束"/>
LinearLayout>

你可能感兴趣的:(移动开发-Android)