我怎样才能知道在Android中滑动viewPager
在我的应用程序中,我有viewPager
,进入这viewPager
我显示视频,我将此视频设置为ViewPagerAdaper
。
我想要在右侧向左滑动时停止视频!
我怎样才能知道在Android中滑动viewPager
我下面写代码在adapter
作秀视频:
@Override
public Object instantiateItem(ViewGroup container, final int position) {
View view = this.inflater.inflate(R.layout.row_coming_soon, container, false);
ButterKnife.bind(this, view);
final ImageView comingSoonCover = (ImageView) view.findViewById(R.id.row_comingSoonCoverImg);
final ImageView comingSoonPlayBtn = (ImageView) view.findViewById(R.id.row_comingSoonPlayBtn);
final VideoView comingSoonVideoPlayer = (VideoView) view.findViewById(R.id.row_comingSoon_videoPlayer);
final RelativeLayout comingSoonImagesLay = (RelativeLayout) view.findViewById(R.id.row_comingSoonImagesLay);
comingSoonPlayBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
comingSoonVideoPlayer.setVideoURI(Uri.parse(model.get(position).getTrailerDirectLink()));
comingSoonVideoPlayer.start();
comingSoonImagesLay.setVisibility(View.GONE);
}
});
我要当刷卡viewPager
,如果comingSoonVideoPlayer.isPlaying();
,停止它和轻扫viewPager
。
我该怎么办?
你可以尝试创建适配器站和星的方法,例如
private void start(int position) {
if(comingSoonVideoPlayer != null {
comingSoonVideoPlayer.setVideoURI(Uri.parse(model.get(position).getTrailerDirectLink()));
comingSoonVideoPlayer.start();
}
}
private void stop() {
if(comingSoonVideoPlayer != null) {
comingSoonVideoPlayer.stop();
}
}
然后在片段
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
mViewPager.getAdapter().stop();
mViewPager.getAdapter().start(position);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
编辑:
让comingSoonVideoPlayer
为班级字段:
final VideoView comingSoonVideoPlayer;
@Override
public Object instantiateItem(ViewGroup container, final int position) {
View view = this.inflater.inflate(R.layout.row_coming_soon, container, false);
ButterKnife.bind(this, view);
final ImageView comingSoonCover = (ImageView) view.findViewById(R.id.row_comingSoonCoverImg);
final ImageView comingSoonPlayBtn = (ImageView) view.findViewById(R.id.row_comingSoonPlayBtn);
comingSoonVideoPlayer = (VideoView) view.findViewById(R.id.row_comingSoon_videoPlayer);
final RelativeLayout comingSoonImagesLay = (RelativeLayout) view.findViewById(R.id.row_comingSoonImagesLay);
comingSoonPlayBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
comingSoonVideoPlayer.setVideoURI(Uri.parse(model.get(position).getTrailerDirectLink()));
comingSoonVideoPlayer.start();
comingSoonImagesLay.setVisibility(View.GONE);
}
});
}
希望这会有所帮助。
对不起,我的英语。
我觉得这个听众可以HALP你
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
现在它已过时。必须使用替代addOnPageChangeListener/removeOnPageChangeListener
我应该把你的代码写入我的片段,但我把视频控制到适配器中!我怎么能把它放入你的代码?你能帮我,给我发代码吗?请 –
Somethins like this onPageSelected {mAdapter.stopOtherPages(position);} –
找不到'stopOtherPages'。请给我这个代码。我想当ssipe viewPager,停止视频 –
谢谢我的朋友,我想尝试你的代码,但我不能!因为我应该将** VideoView **初始化为'instantiateItem',比如:'VideoView'comingSoonVideoPlayer =(VideoView)view.findViewById(R.id.coming_pay_videoPlayer);'。当初始化** VideoView **全球使用其他方面,不工作我videoView到我的应用程序!我该如何解决它?为了使用您的代码,我应该初始化** VideoView **全局,但我无法在全局初始化这个!为什么? –
请在邮编#1中查看我的代码以了解我的意思。请帮助我的朋友,请 –
当您尝试将'comingSoonVideoPlayer'作为班级字段时,出现什么错误? – Cochi