你可以使用监听器。
mPager.setOnPageChangeListener(new OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
if(mPageSelectedListener!=null){
mPageSelectedListener.pageSelected(position);
}
}
});
您像这样定义 PageSelectedListener 的地方
public interface PageSelectedListener{
public void pageSelected(int position);
};
public void setPageSelectedListener(PageSelectedListener l){
mPageSelectedListener = l;
}
并在您的片段中像这样使用它
if(getActivity() instanceof MyActivity
((MyActivity)getActivity()).setPageSelectedListener(new PageSelectedListener(){
@Override
public void pageSelected(int position) {
if(position==MyAdapter.MY_PAGE){
// do something with currently viewed page...like resume it
} else {
// do something with any other page..like pause it
}
}
});
}