Get current view of ViewFlipper while it is flipping

Gabrielle picture Gabrielle · Dec 6, 2011 · Viewed 8k times · Source

There is any way to know want view is displayed on ViewFlipper while it is flipping? I have this problem : my ViewFlipper contains some images and one video. I want that when viewflipper display the Videoview with video, ViewFlipper to stop flipping and Video to start playing. This works only when video is displayed first time. After that, video is displayed as an image and I don't know how to catch the moment when video is displayed. Can anyone help me, pleasee?

Here is how I populate ViewFlipper :

private void populate() {
        for (int i = 0; i < jArray.length(); i++) {
            System.out.println("lungime " + jArray.length());
            LinearLayout l = new LinearLayout(this);
            l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            l.setBackgroundColor(0x000000);
            l.setOrientation(LinearLayout.VERTICAL);
            vf.addView(l);

            File f = new File(Environment.getExternalStorageDirectory()
                    + "/Images" + "/");

            File[] files = f.listFiles();

            if (files[i].getName().equals("Video" + idPhotos[i])) {
                System.out.println("Video to Viewflipper");
                System.out.println("Video Path " + files[i].getPath());
                myVideoView = new VideoView(this);
                myVideoView.setVideoPath(files[i].getPath());
                myVideoView.requestFocus();
                myVideoView.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                l.setBackgroundColor(Color.BLACK);
                myVideoView.setKeepScreenOn(true);

                myVideoView.setOnPreparedListener(new OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        vf.stopFlipping();
                    }

                });
                myVideoView.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            downXValue = event.getX();
                            break;
                        }

                        case MotionEvent.ACTION_UP: {
                            currentX = event.getX();
                            if (downXValue == currentX) {
                                Intent intent = new Intent(getBaseContext(),
                                        FinActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(intent);
                                finish();
                            }
                        }
                        }
                        return true;
                    }

                });

                myVideoView
                        .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            public void onCompletion(MediaPlayer mp) {
                                vf.startFlipping();
                            }
                        });

                vf.stopFlipping();
                myVideoView.start();
                myVideoView.setId(i);
                l.addView(myVideoView);
                l.setId(i);
            }

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 3;
            options.inPurgeable = true;

            bitmap = BitmapFactory.decodeFile(files[i].getPath());
            img = new ImageView(this);
            img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));

            img.setImageBitmap(bitmap);
            img.setOnTouchListener(this);
            l.addView(img);
            l.setId(i);
            img = null;
        }
    }

and on onCreate() method I have this :

if (ff.exists() && (files.length == jArray.length())) {     
                populate();
                vf.setOutAnimation(AnimationUtils.loadAnimation(
                        getBaseContext(), R.anim.push_left_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(
                        getBaseContext(), R.anim.push_left_in));
                vf.startFlipping();
}

(I call populate() method only once a time, maybe this is the problem)

Any idea is welcome. Thanks in advance.

Answer

blessenm picture blessenm · Dec 6, 2011

Im not sure if this will work but it is worth a try.

Add an animation listener to the animation object you set as the viewflipper's 'IN' animation. Overide the listeners onAnimationEnd method, and inside that method check

vf.getDisplayedChild();

The above code will give index of the currently displayed child. So if its in the videoview, you can stop flipping and play the video.