3

I'm using a ViewPager to show several Fragments provided by a FragmentPagerAdapter. One of these Fragments shows a VideoView and an according MediaController.

The issue I'm facing is, that the MediaController is not swiping to the left/right as it's supposed to together with the rest of the Fragment. (Hopefully you can follow me ^^)

I've looked through a lot of similar discussions on StackOverflow, but unfortunatly none of them fixes my issue.

This is my xml for the Fragment that is instanciated by the adapter:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/vv_project_details"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

This is code belongs to onCreateView of the Fragment:

    final VideoView vv = (VideoView) rootView
            .findViewById(R.id.vv_project_details);
    vv.setVideoURI(Uri uri = new Uri("some-uri"));
    mMediaController = new MediaController(getActivity(), false); // should I use rootView.getContext() instead of getActivity()?
    vv.setMediaController(mMediaController);

    vv.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
            mMediaController.setAnchorView(vv);
            mMediaController.show(0);

        }
    });

Everything is working fine, except for swiping through my Fragments, as the MediaController appears fixed... :-/

Edit: this is a screenshot made while swiping between 2 Fragments. As you can see, the MediaController remains at its initial position...

Screenshot showing that the Mediacontroller remains at its inital position while swiping

FranBran
  • 1,017
  • 2
  • 11
  • 32
  • I also tried to generate the `VideoView` programmatically because I thought there might is problam with having multiple `VideoViews` holding the same Ids in different `Fragments`, but that didn't change anything as well... – FranBran Mar 22 '14 at 13:04

1 Answers1

1

I had the same problem and I do the following to manually hide/show the controller:

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            if (position == Tabs.NOWPLAYING.ordinal())
                tabPagerAdapter.getNowPlayingFragment().show();
            else 
                tabPagerAdapter.getNowPlayingFragment().hide();

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

It needs refactoring though as it doesn't check if controller is actually showing before trying to hide. It matters if you have more then two tabs.

aycanadal
  • 1,106
  • 2
  • 15
  • 42