Listener for ViewFlipper widget flipping events

前端 未结 5 1545
情话喂你
情话喂你 2020-12-09 09:12

I have a ViewFlipper implementation that needs to be improved. This ViewFlipper has three child views. Basically, I want an indicator on which child view is currently active

5条回答
  •  情话喂你
    2020-12-09 09:47

    While this is an old question I found a decent approach that works.

    public class MainLaunch extends Activity {
    
        ... main setup and code
    
        int currentIndex = 0;
        int maxIndex = 3;
    
        // set specific animations for the view flipper on showNext
        // only showNext while in valid index
        public void showNext() {
            if( currentIndex < maxIndex )
            {
                currentIndex++;
                viewFlipper.setInAnimation(getBaseContext(), R.anim.slide_in_left);
                viewFlipper.setOutAnimation(getBaseContext(), R.anim.slide_out_right);
                viewFlipper.showNext();
            }
        }
    
        // set specific animations for the view flipper on showPrevious
        // only showPrevious while in valid index
        public void showPrevious() {
            if( currentIndex > 0 )
            {
                currentIndex--;
                viewFlipper.setInAnimation(getBaseContext(), R.anim.slide_in_right);
                viewFlipper.setOutAnimation(getBaseContext(), R.anim.slide_out_left);
                viewFlipper.showPrevious();
            }
        }
    
        // get current flipped view
        public View getCurrentView() {
            return viewFlipper.getChildAt(currentIndex);
        }
    
    
    }
    

    Then to use the ViewFlipper you call showNext() or showPrevious anywhere and can get the currently active view by calling getCurrentView(). This helps in setting different animations for left and right flipping and for easily getting current working views.

提交回复
热议问题