轮播图小Demo

package com.example.shengxin.scrollerdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    AutoScrollViewPager asvp;

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

        asvp = (AutoScrollViewPager) findViewById(R.id.asvp);
        asvp.setShouldStop(false);
        int[] data = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
        asvp.setData(data);
    }

    public void click(View v) {
        int[] data = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
        asvp.setData(data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        asvp.setShouldStop(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        asvp.setShouldStop(true);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        asvp.setShouldStop(false);
    }
}
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:onClick="click"
        android:text="jiazai" />

    <com.example.shengxin.scrollerdemo.AutoScrollViewPager
        android:id="@+id/asvp"
        android:layout_width="match_parent"
        android:layout_height="200dp"></com.example.shengxin.scrollerdemo.AutoScrollViewPager>
</RelativeLayout>

package com.example.shengxin.scrollerdemo;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * Created by shengxin on 17/11/27.
 */

public class AutoScrollViewPager extends RelativeLayout implements Runnable {
    private ViewPager vp;
    private Handler handler = new Handler();
    private int[] data;

    public AutoScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_autoscroll, this);
        //初始化控件
        vp = (ViewPager) findViewById(R.id.vp);
        /*vp.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });*/
    }

    public void setData(final int[] data) {
        this.data = data;
        if (data == null) {
            return;
        }
        vp.setAdapter(new PagerAdapter() {

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = new ImageView(container.getContext());
                imageView.setImageResource(data[position]);
                container.addView(imageView);
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public int getCount() {
                return data.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        });

        handler.postDelayed(this, 2000);
    }

    private boolean rightDirection;
    public boolean shouldStop ;

    public void setShouldStop(boolean shouldStop) {
        this.shouldStop = shouldStop;
        if (shouldStop == false) {
            handler.postDelayed(this, 2000);
        }
    }


    @Override
    public void run() {
        if (shouldStop) {
            return;
        }
        int currentItem = vp.getCurrentItem();
        if (currentItem == 0) {
            vp.setCurrentItem(currentItem + 1);
            rightDirection = true;
        } else if (currentItem == data.length - 1) {
            vp.setCurrentItem(currentItem - 1);
            rightDirection = false;
        } else if (rightDirection) {
            vp.setCurrentItem(currentItem + 1);
        } else {
            vp.setCurrentItem(currentItem - 1);
        }
        handler.postDelayed(this, 2000);
    }
}

 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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


</RelativeLayout>

 
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值