My setAnimationListener on my viewFlipper keeps running the overlay before 
the animation is complete. Here is my fragment, I think I am just not 
calling my listener correctly but not entirely sure if it will work since i 
have multiple animations running.

Basically I have a viewFlipper that acts as a random category selector when 
you swipe, once it stops on a category I then open an overlay with some 
info then wait 3 seconds and load a new fragment but things are just not 
working right. If you have any other ideas I can try I would appreciate the 
help! Thanks!

public class CatFragment extends Fragment {

    private ViewFlipper mViewFlipper;
    private int mSpeed;
    private int mCount;
    private int mFactor;
    private boolean mAnimating;
    String category;
    private Handler handler = new Handler();
    TextView catResult;
    View topLevelLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.category_selector, container, 
false);

        mViewFlipper = (ViewFlipper)view.findViewById(R.id.view_flipper);
        mAnimating = false;
        mCount = 0;
        mSpeed = 0;
        mViewFlipper.setClipToPadding(false);
        mViewFlipper.setClipChildren(false);
        mViewFlipper.setPadding(40,0,40,0);            

        //Set overlay and make invisible
        topLevelLayout = view.findViewById(R.id.top_layout);
        topLevelLayout.setVisibility(View.INVISIBLE);

        final GestureDetector gesture = new GestureDetector(getActivity(), new 
GestureDetector.SimpleOnGestureListener() {

            @Override
            public boolean onDown(MotionEvent e) {
                // TODO Auto-generated method stub
                return true;
            }

            private Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    right();
                    if (mCount<1) {
                        mAnimating = false;
                    } else {
                        Handler h = new Handler();
                        h.postDelayed(r1, mSpeed);
                    }
                }

            };

            private Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    left();
                    if (mCount<1) {
                        mAnimating = false;
                    } else {
                        Handler h = new Handler();
                        h.postDelayed(r2, mSpeed);
                    }
                }

            };

            @Override
            public boolean onFling(MotionEvent start, MotionEvent finish, float 
xVelocity, float yVelocity) {
                try {
                    if (mAnimating) return true;
                    mAnimating = true;                    
                    mCount = (int) Math.abs(xVelocity) / 300;
                    mFactor = (int) 300 / mCount;
                    mSpeed = mFactor;
                    if (xVelocity>0) {
                        //Left
                        Handler h = new Handler();
                        h.postDelayed(r2, mSpeed);
                    } else {
                        //Right
                        Handler h = new Handler();
                        h.postDelayed(r1, mSpeed);
                    }
                    
((TextView)getActivity().findViewById(R.id.velocity)).setText("VELOCITY => 
"+Float.toString(xVelocity));
                } catch (ArithmeticException e) {
                    //swiped too slow doesn't register
                    mAnimating = false;
                }
                return true;
            }

            //In From Right
            private void right() {
                mCount--;
                mSpeed+=mFactor;
                Animation inFromRight = new TranslateAnimation(
                        Animation.RELATIVE_TO_PARENT, +1.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f);
                inFromRight.setInterpolator(new LinearInterpolator());
                inFromRight.setDuration(mSpeed);
                Animation outToLeft = new TranslateAnimation(
                        Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, -1.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f);
                outToLeft.setInterpolator(new LinearInterpolator());
                outToLeft.setDuration(mSpeed);
                mViewFlipper.clearAnimation();
                mViewFlipper.setInAnimation(inFromRight);
                mViewFlipper.setOutAnimation(outToLeft);
                if (mViewFlipper.getDisplayedChild()==0) {
                    mViewFlipper.setDisplayedChild(5);
                } else {
                    mViewFlipper.showPrevious();
                }
                mViewFlipper.getOutAnimation().setAnimationListener(new 
Animation.AnimationListener() {
                    public void onAnimationStart(Animation animation) {}
                    public void onAnimationRepeat(Animation animation) {}
                    public void onAnimationEnd(Animation animation) {
                        //after animation end
                        categorySwitch();
                    }
                });
                
((TextView)getActivity().findViewById(R.id.counter)).setText("COUNTER => 
"+Integer.toString(mCount));
                
((TextView)getActivity().findViewById(R.id.speed)).setText("SPEED => 
"+Integer.toString(mSpeed));
            }

            //In From Left
            private void left() {
                mCount--;
                mSpeed+=mFactor;
                Animation inFromLeft = new TranslateAnimation(
                        Animation.RELATIVE_TO_PARENT, -1.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f);
                inFromLeft.setInterpolator(new AccelerateInterpolator());
                inFromLeft.setDuration(mSpeed);
                Animation outToRight = new TranslateAnimation(
                        Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, +1.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f,
                        Animation.RELATIVE_TO_PARENT, 0.0f);
                outToRight.setInterpolator(new AccelerateInterpolator());
                outToRight.setDuration(mSpeed);
                mViewFlipper.clearAnimation();
                mViewFlipper.setInAnimation(inFromLeft);
                mViewFlipper.setOutAnimation(outToRight);
                if (mViewFlipper.getDisplayedChild()==0) {
                    mViewFlipper.setDisplayedChild(5);
                } else {
                    mViewFlipper.showPrevious();
                }
                mViewFlipper.getOutAnimation().setAnimationListener(new 
Animation.AnimationListener() {
                    public void onAnimationStart(Animation animation) {}
                    public void onAnimationRepeat(Animation animation) {}
                    public void onAnimationEnd(Animation animation) {
                        //after animation end
                        categorySwitch();
                    }
                });
                
((TextView)getActivity().findViewById(R.id.counter)).setText("COUNTER => 
"+Integer.toString(mCount));
                
((TextView)getActivity().findViewById(R.id.speed)).setText("SPEED => 
"+Integer.toString(mSpeed));
            }

            @Override
            public void onLongPress(MotionEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float 
distanceX, float distanceY) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {
                // TODO Auto-generated method stub

            }

        });

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gesture.onTouchEvent(event);
            }
        });

        return view;

    }

    public void categorySwitch(){

        //Set fragment null
        Fragment fragment = null;

        switch (mViewFlipper.getDisplayedChild()){
            case 0:
                fragment = new QuestionActivity();
                category = "Cat1";
                break;
            case 1:
                fragment = new QuestionActivity();
                category = "Cat2";
                break;
            case 2:
                fragment = new QuestionActivity();
                category = "Cat3";
                break;
            case 3:
                fragment = new QuestionActivity();
                category = "Cat4";
                break;
            case 4:
                fragment = new QuestionActivity();
                category = "Cat5";
                break;
            case 5:
                fragment = new QuestionActivity();
                category = "Cat6";
                break;
            default:
                fragment = new HomeFragment();
                break;
        }

        //Show category name
        catResult = (TextView)getActivity().findViewById(R.id.catResult);
        catResult.setText(category);

        //Show Overlay
        topLevelLayout.setVisibility(View.VISIBLE);

        //Open New fragment
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Do something after 3500ms

                //Hide Overlay
                topLevelLayout.setVisibility(View.INVISIBLE);

                // Create new fragment and transaction
                

                Bundle b = new Bundle(); // passing the score
                android.support.v4.app.FragmentTransaction ft = 
getFragmentManager().beginTransaction();
                b.putString("category", category); // Your score
                newFragment.setArguments(b);// Put your score to your next

                // Replace whatever is in the fragment_container view with this 
fragment,
                // and add the transaction to the back stack
                ft.replace(R.id.frame, newFragment);
                ft.addToBackStack(null);

                // Commit the transaction
                ft.commit();
            }
        }, 3500);
    }

}



-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/185740c4-0b90-4f29-9377-9b9099c4d7ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to