Code example for ViewFlipper

Methods: getChildAt, getDisplayedChild, setInAnimation, setOutAnimation, showNext, sleep

0
                this.flickOrTaptoDate(currentX, currentX - lastTouchX
                        , currentY, currentY - lastTouchY);
                break; 
        } 
        try { 
            Thread.sleep(16);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } 
        return true; 
        //return super.onTouchEvent(event); 
    } 
    /** 
     * フリックかタップかにより画面を変えます 
     * @param currentX 現在のX座標 
     * @param velocityX X座標の移動量 
     * @param currentY 現在のY座標 
     * @param velocityY Y座標の移動量 
     */ 
    private boolean flickOrTaptoDate(float currentX, float velocityX
            , float currentY, float velocityY) {
         
        //基準の移動距離はViewConfigurationを使用する 
        int minimumFlingVelocity =  ViewConfiguration.getMinimumFlingVelocity();
 
        velocityX = Math.abs(velocityX);
        velocityY = Math.abs(velocityY);
         
        if ( velocityX < minimumFlingVelocity && 
             velocityY < minimumFlingVelocity  ) {
             
            //変量 < 閾値の場合タップ 
            MonthlyCalendarView currentView = 
                (MonthlyCalendarView)viewFlipper.getCurrentView();
            if ( currentView != null ) {
                boolean result = currentView.performClick(); //対象ビューのクリックを発生させる
                performSelectionCalendar(currentView);
                return result;
            } 
        } else { 
            //変量 >= 閾値の場合フリック 
 
            //X軸とY軸の変量の大きい方にアニメーション 
            int direction = ( velocityY >= velocityX ) 
                ? DIRECTION_VERTICAL 
                : DIRECTION_HORIZONTAL;
 
            //次月(上、又は左にモーション)? 前月(下又は右にモーション)? 
            boolean showNext = ( direction == DIRECTION_VERTICAL ) 
                ? ( currentY < lastTouchY )
                : ( currentX < lastTouchX ) ;
             
            //カレンダー加減算 
            calendar.add(Calendar.MONTH, showNext ? 1 : -1);
             
            //適切な月に移動 
            if ( showNext ) {
                showNextMonth(direction);
            } else { 
                showPreviousMonth(direction);
            } 
        } 
        return true; 
    } 
    /** 
     * カレンダーの特定日が選択された際の処理を記述します 
     * @param currentView 対象になっているカレンダービューをセットします 
     */ 
    protected void performSelectionCalendar(MonthlyCalendarView currentView) {
        DateInfo dateInfo = currentView.getSelectedDateInfo();
         
        if ( dateInfo != null ) {
            //Log.d(TAG, "onCalendarSelection: dateInfo=" + dateInfo ); 
            int size = this.listenerList.size();
            if ( size > 0 ) {
                for (int i = size-1; i >= 0; i--) {
                    this.listenerList.get(i).onCalendarSelection(
                            new CalendarSelectionEvent(this, dateInfo));
                } 
            } 
        } 
         
    } 
    /** 
     * カレンダーの特定日を選択します 
     * @param year 年をセット 
     * @param month 月をセット 
     * @param day 日をセット 
     */ 
    public void selectCalendar(int year, int month, int day) {
        MonthlyCalendarView current = 
            (MonthlyCalendarView)this.viewFlipper.getCurrentView();
        if ( current != null ) {
            current.setCalendar(year, month, day);
            DateInfo dateInfo = current.getSelectedDateInfo();
            //Log.d(TAG, "onCalendarSelection: dateInfo=" + dateInfo ); 
            int size = this.listenerList.size();
            if ( size > 0 ) {
                for (int i = size-1; i >= 0; i--) {
                    this.listenerList.get(i).onCalendarSelection(
                            new CalendarSelectionEvent(this, dateInfo));
                } 
            } 
        } 
    } 
    /** 
     * 現在選択されている日付情報を取得します 
     * @return MonthlyCalendarView.DateInfo 選択されている日付情報が戻ります 
     */ 
    public DateInfo getDateInfo() { 
        MonthlyCalendarView currentView = 
            (MonthlyCalendarView)viewFlipper.getCurrentView();
        return currentView.getSelectedDateInfo();
    } 
    /** 
     * カレンダ選択監視リスナを追加します 
     * @param listener リスナをセット 
     */ 
    public void addOnCalendarSelectionListener(OnCalendarSelectionListener listener) {
        this.listenerList.add(listener);
    } 
    /** 
     * カレンダ選択リスナを取り除きます 
     * @param listener リスナをセット 
     */ 
    public void removeOnCalendarSelectionListener(OnCalendarSelectionListener listener) {
       this.listenerList.remove(listener); 
    } 
    /** 
     * 年、月を設定します 
     */ 
    protected void setYearAndMonthFromDialog() { 
         
        Context ctx = this.getContext();
        Resources res = ctx.getResources();
        LayoutInflater inflator = LayoutInflater.from(ctx);
 
        final View yearMonthView = inflator.inflate(R.layout.year_month_pair, null);        
        final NumberPicker yearPicker = (NumberPicker) yearMonthView.findViewById(R.id.pickerYear);
        final NumberPicker monthPicker = (NumberPicker) yearMonthView.findViewById(R.id.pickerMonth);
         
         
        MonthlyCalendarView cView = 
            (MonthlyCalendarView)this.viewFlipper.getChildAt(this.viewFlipper.getDisplayedChild());
        final int currentYear = cView.getCalendar().get(Calendar.YEAR);
        final int currentMonth = cView.getCalendar().get(Calendar.MONTH)+1;//月は0オリジン
 
        yearPicker.setRange(currentYear-1, currentYear+1);
        yearPicker.setSpeed(100);
        yearPicker.setCurrent(currentYear);
 
         
        monthPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
        DateFormatSymbols dfs = new DateFormatSymbols();
        String[] months = new String[12];
        System.arraycopy(dfs.getShortMonths(), 0, months, 0, 12);
        /* 
        if (months[0].startsWith("1")) { 
            for (int i = 0; i < months.length; i++) { 
                months[i] = String.valueOf(i + 1); 
            } 
        } 
        */ 
        monthPicker.setRange(1, 12, null);
        monthPicker.setSpeed(200);
        monthPicker.setCurrent(currentMonth);
         
         
        AlertDialog.Builder builder = 
            new AlertDialog.Builder(this.getContext());
        //年月を設定します 
        builder.setTitle(res.getString(R.string.year_month_dialog_title))
            .setCancelable(true) 
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override 
                public void onClick(DialogInterface dialog, int which) {
                    //入力された年月をビューに設定 
                    int setYear = yearPicker.getCurrent();
                    int setMonth = monthPicker.getCurrent(); 
                     
                    if ( currentYear != setYear || currentMonth != setMonth ) {
                        showYearMonth(setYear, setMonth);
                    } 
                } 
             }) 
            .setView(yearMonthView)
            .show(); 
        monthPicker.requestFocus();
    } 
    /** 
     * 前月を表示する 
     * @param direction モーションの方向をセット(DIRECTION_VERTICAL|DIRECTION_HORIZONTAL) 
     */ 
    private void showPreviousMonth(int direction) {
        int index = this.viewFlipper.getDisplayedChild() == 0 ? 1 : 0;
        MonthlyCalendarView calendarView = 
            (MonthlyCalendarView)this.viewFlipper.getChildAt(index);
        if ( calendarView != null ) {
            calendarView.setCalendar(this.calendar);
            this.setHeader(this.calendar.get(java.util.Calendar.YEAR)
                    , calendar.get(java.util.Calendar.MONTH));
            if ( direction == DIRECTION_VERTICAL ) {
                this.viewFlipper.setInAnimation(inFromTop);
                this.viewFlipper.setOutAnimation(outToBottom);
            } else { 
                this.viewFlipper.setInAnimation(inFromLeft);
                this.viewFlipper.setOutAnimation(outToRight);
            } 
            this.viewFlipper.showPrevious();
        } 
    } 
    /** 
     * 次月を表示する 
     * @param direction モーションの方向をセット(DIRECTION_VERTICAL|DIRECTION_HORIZONTAL) 
     */ 
    private void showNextMonth(int direction) {
        int index = this.viewFlipper.getDisplayedChild() == 0 ? 1 : 0;
        MonthlyCalendarView calendarView = 
            (MonthlyCalendarView)this.viewFlipper.getChildAt(index);
        if ( calendarView != null ) {
            calendarView.setCalendar(this.calendar);
            this.setHeader(this.calendar.get(java.util.Calendar.YEAR)
                    , calendar.get(java.util.Calendar.MONTH));
             
            if ( direction == DIRECTION_VERTICAL ) {
                this.viewFlipper.setInAnimation(inFromBottom);
                this.viewFlipper.setOutAnimation(outToTop);
            } else { 
                this.viewFlipper.setInAnimation(inFromRight);
                this.viewFlipper.setOutAnimation(outToLeft);
            } 
            this.viewFlipper.showNext();
        } 
    } 
    /** 
     * 対象の年月を表示します 
     * @param year 年をセット