【Android UI】ViewFlipper实现广告轮播图

江湖老规矩,先看示例图

【Android UI】ViewFlipper实现广告轮播图_第1张图片


1、为ViewFlipper实现切换监听器

public class BlueViewFlipper extends ViewFlipper {

    private OnDisplayChangedListener mListener;

    public BlueViewFlipper(Context context) {
        super(context);
    }

    public BlueViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnDisplayChagnedListener(OnDisplayChangedListener listener) {
        if (mListener != listener) {
            this.mListener = listener;
        }
    }

    @Override
    public void showNext() {
        super.showNext();
        if(mListener != null){
            mListener.OnDisplayChildChanging(this, super.getDisplayedChild());
        }
    }

    @Override
    public void showPrevious() {
        super.showPrevious();
        if(mListener != null){
            mListener.OnDisplayChildChanging(this, super.getDisplayedChild());
        }
    }

    public interface OnDisplayChangedListener {
        void OnDisplayChildChanging(ViewFlipper view, int index);
    }
}

2、MainActivity.java

public class MainActivity extends ActionBarActivity {

    private BlueViewFlipper viewFlipper;
    private float startX;
    //定时任务
    private ScheduledExecutorService executorService;
    private LinearLayout dotLL;
    private ImageView dotIV;
    private List ivs;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            viewFlipper.setInAnimation(MainActivity.this, R.anim.in_from_right);
            viewFlipper.setOutAnimation(MainActivity.this, R.anim.out_from_left);
            viewFlipper.showPrevious();

        }
    };

    public MainActivity() {
    }

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

        init();
        setListener();

        startPlay();
    }

    private void startPlay(){
        executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleAtFixedRate(new SlideShowTask(), 1, 3, TimeUnit.SECONDS);
    }

    private void stopPlay(){
        executorService.shutdown();
    }

    private void init() {
        this.viewFlipper = (BlueViewFlipper) this.findViewById(R.id.viewFlipper);
        this.dotIV = (ImageView) this.findViewById(R.id.dotIV);
        this.dotLL = (LinearLayout) this.findViewById(R.id.dotLL);
        ViewGroup.LayoutParams layoutParams = this.dotIV.getLayoutParams();
        this.ivs = new ArrayList<>();
        this.dotLL.removeAllViews();
        for(int i=0; i startX) {
                            viewFlipper.setInAnimation(MainActivity.this, R.anim.in_from_left);
                            viewFlipper.setOutAnimation(MainActivity.this, R.anim.out_from_right);
                            viewFlipper.showNext();
                        }else if(event.getX() < startX) {
                            viewFlipper.setInAnimation(MainActivity.this, R.anim.in_from_right);
                            viewFlipper.setOutAnimation(MainActivity.this, R.anim.out_from_left);
                            viewFlipper.showPrevious();
                        }
                        break;
                }
                return true;
            }
        });
        viewFlipper.setOnDisplayChagnedListener(new BlueViewFlipper.OnDisplayChangedListener() {
            @Override
            public void OnDisplayChildChanging(ViewFlipper view, int index) {
                Log.i("Blue", "Current Item: " + index + "");
                for(int i = 0; i < ivs.size(); i++) {
                    if((ivs.size() - i - 1) == index) {
                        ivs.get(i).setImageResource(R.drawable.dot_focus);
                    }else {
                        ivs.get(i).setImageResource(R.drawable.dot_default);
                    }
                }

            }
        });
    }
    
    private class SlideShowTask implements Runnable{
        @Override
        public void run() {
            // TODO Auto-generated method stub
            synchronized (viewFlipper) {
                mHandler.obtainMessage().sendToTarget();
            }
        }

    }
}

3、activity_main.xml



    

        

            

            

            

        

        

            

        

    



你可能感兴趣的:(Java,Android)