@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flash_card); // ----create cards----------- createCardLayout(); mTextViewWord = (TextView) findViewById(R.id.tv_word); mTextViewDefinition = (TextView) findViewById(R.id.tv_def); // --------Handle ViewAnimator control------------ mViewAnimator = (ViewAnimator) findViewById(R.id.flipper); mViewAnimator.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (mIsOver & mIsWordShowing) return; flipTransition(mViewAnimator, FlipDirection.RIGHT_LEFT); } }); // ------Initialize variables after activity reloads---------- if (savedInstanceState == null) { this.InitializeArrays(); // Loads data into mWordArray and initializes mWordIdArray mIsWordShowing = true; // if mIsWordShowing = false, it means definition is showing if (!mWordArray.isEmpty()) { mIsOver = false; Random random = new Random(); int index = random.nextInt(mWordArray.size()); Word word = mWordArray.get(index); mWordArray.remove(index); mWordIdArray.add(word.getId()); changeText(word); } else { // When the word list is empty, shows instruction message mIsOver = true; mTextViewWord.setTextSize((float) 16); mTextViewWord.setText(R.string.str_instruct_msg); } } else { // reload saved states mIsWordShowing = savedInstanceState.getBoolean("mIsWordShowing"); mIsOver = savedInstanceState.getBoolean("mIsOver"); mWordIdArray = savedInstanceState.getIntegerArrayList("mWordIdArray"); mCurrentCard = savedInstanceState.getInt("mCurrentCard"); mViewAnimator.setDisplayedChild(mCurrentCard); // removes words that were already shown before the activity reloads // and reloads the last word that was shown for (int i = 0; i < mWordIdArray.size(); i++) { for (int j = 0; j < mWordArray.size(); j++) { if (mWordIdArray.get(i) == mWordArray.get(j).getId()) { if (i == mWordIdArray.size() - 1) { // when it goes to the last id in the mWorIdArray changeText( mWordArray.get( j)); // reload the last word that was shown before the activity reloads } mWordArray.remove(j); break; } } } } }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (mWord == null && savedInstanceState != null) { mWord = (CPDecksVocabulary) savedInstanceState.getSerializable(WORD); } if (mWord == null) { getActivity().finish(); return null; } super.onCreateView(inflater, container, savedInstanceState); View v = inflater.inflate(R.layout.flashcard_flipper, null); mViewAnimator = (ViewAnimator) v.findViewById(R.id.viewFlipper); mViewAnimator.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { flipIt(); } }); v.findViewById(R.id.FlaschardScrollView) .setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { flipIt(); } }); v.findViewById(R.id.FlaschardScrollView) .setOnTouchListener( new OnTouchListener() { private float startX; private float startY; private boolean isAClick(float startX, float endX, float startY, float endY) { float differenceX = Math.abs(startX - endX); float differenceY = Math.abs(startY - endY); if (differenceX > 5 || differenceY > 5) { return false; } return true; } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX = event.getX(); startY = event.getY(); break; case MotionEvent.ACTION_UP: { float endX = event.getX(); float endY = event.getY(); if (isAClick(startX, endX, startY, endY)) { flipIt(); // WE HAVE A CLICK!! return true; } break; } } return false; } }); mRowsFront[0] = (AutoResizeTextView) v.findViewById(R.id.firstRowFront); mRowsFront[1] = (AutoResizeTextView) v.findViewById(R.id.secondRowFront); mRowsFront[2] = (AutoResizeTextView) v.findViewById(R.id.thirdRowFront); mRowsBack[0] = (AutoResizeTextView) v.findViewById(R.id.firstRowBack); mRowsBack[1] = (AutoResizeTextView) v.findViewById(R.id.secondRowBack); mRowsBack[2] = (AutoResizeTextView) v.findViewById(R.id.thirdRowBack); mPlayButtonFront = (Button) v.findViewById(R.id.wordPlayButtonFront); mPlayButtonBack = (Button) v.findViewById(R.id.wordPlayButtonBack); mImageFront = (ImageView) v.findViewById(R.id.wordImageFront); return v; }