试图了解onPageScrolled

时间:2016-12-15 11:59:58

标签: java android android-viewpager

我有viewpager VideoView

所以我想滚动当前页面时视频暂停。这个东西我在onPageScrolled做的但我遇到了一个问题,因为这个方法被多次调用。

我搜索了android官方文档,但我一无所获。

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


    VideoView videoView = (VideoView) viewPager.findViewWithTag("videoView"+position);
    if(videoView!=null){
        Log.d(TAG,"onPageScrolled "+position);
        videoView.pause();
    }

}

@Override
public void onPageSelected(int position) {
    //Toast.makeText(getApplicationContext(),"onPageSelected "+position,Toast.LENGTH_SHORT).show();
    View view = (View) viewPager.findViewWithTag("view" + position);
    if(view!=null) {

        //ImageView thumbnail = (ImageView) view.findViewById(R.id.fullScreenVideoThumbnail);
        //thumbnail.setVisibility(View.GONE);

        Log.d(TAG,"onPageSelected Position: "+position);
        VideoView videoView = (VideoView) view.findViewById(R.id.fullScreenVideoFullVideo);
        videoView.bringToFront();
        videoView.start();
    }



}

@Override
public void onPageScrollStateChanged(int state) {
    //Toast.makeText(getApplicationContext(),"onPageScrollStateChanged",Toast.LENGTH_SHORT).show();
}

});

1 个答案:

答案 0 :(得分:0)

您可以使用videoView!= null进行另一项检查,以检查视频是否已暂停。如果它什么也不做,如果不是,请暂停它。这将确保无论该方法调用多少次,它只暂停一次视频