我希望在片段更改时停止播放片段中的videoView。 当我更改片段时,视频仍在播放。
以下是主要活动的代码
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentStepOne(), "1");
adapter.addFragment(new FragmentStepTwo(), "2");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
这是Fragment Class
public class FragmentStepOne extends Fragment{
public FragmentStepOne() {
// Required empty public constructor
}
VideoView video;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_step_two, container, false);
video = (VideoView) rootView.findViewById(R.id.videoView);
Uri uri = Uri.parse("android.resource://"+ getActivity().getPackageName()+"/"+ R.raw.video);
video.setVideoURI(uri);
MediaController mc=new MediaController(getContext());
mc.setAnchorView(video);
video.setMediaController(mc);
video.requestFocus();
return rootView;
}
提前谢谢。