MediaController not swiping in ViewPager

落爺英雄遲暮 提交于 2019-12-22 09:36:42

问题


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...


回答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.



来源:https://stackoverflow.com/questions/22564658/mediacontroller-not-swiping-in-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!