How to prevent ViewFlipper from looping

前端 未结 3 1065
梦毁少年i
梦毁少年i 2021-01-22 16:43

I am developing an application in which I am using a ViewFlipper with a custom OnTouch implementation. In the ViewFlipper, I have about 2

相关标签:
3条回答
  • 2021-01-22 17:27

    You know that there are 20 children in the viewflipper. Therefore make an if statement in the onclick that checks if the getDisplayedChild() is lower then 20. If it is 20 or higher then just dont call showNext().

    0 讨论(0)
  • 2021-01-22 17:40

    I detect end of children by custom ViewFlipper

    public class ExViewFlipper extends ViewFlipper {
    
      private OnChangeViewListener mOnChangeViewListener;
    
      public ExViewFlipper(Context context) {
        super(context);
      }
    
      public ExViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      public interface OnChangeViewListener {
    
        /**
         * call on Change view
         * 
         * @param index next index
         * @param hasNext true if next view is exist
         */
        void onChange(int index, boolean hasNext);
      }
    
      public void setOnChangeViewListener(OnChangeViewListener listener) {
        mOnChangeViewListener = listener;
      }
    
      @Override
      public void showNext() {
    
        super.showNext();
    
        if (mOnChangeViewListener != null) {
          mOnChangeViewListener.onChange(getDisplayedChild(), true);
        }
      }
    
      @Override
      public void showPrevious() {
    
        super.showPrevious();
    
        if (mOnChangeViewListener != null) {
          mOnChangeViewListener.onChange(getDisplayedChild(), false);
        }
      }
    
      public boolean isFirstItem() {
    
        return getDisplayedChild() == 0;
      }
    
      public boolean isLastItem() {
    
        return getDisplayedChild() == getChildCount() - 1;
      }
    }
    
    0 讨论(0)
  • 2021-01-22 17:40

    Set an AnimationListener on the Viewflipper and then stop the animation when you reach the last slide.

        viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipperMain);              
        viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.slide_in_right));       
        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_left));   
    
        AnimationListener mAnimationListener = new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {
                //animation started event
            }
    
            public void onAnimationRepeat(Animation animation) {
            }
    
            public void onAnimationEnd(Animation animation) {
                //TODO animation stopped event
                if (viewFlipper.getDisplayedChild()==intLastChild){
                    viewFlipper.stopFlipping();
                }
            }
        };
    
       //Get a reference to one of the animations set on the ViewFlipper
       //In this example, I used the "In Animation"
       viewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
    
    0 讨论(0)
提交回复
热议问题