首先,我在第一个video
中播放view pager
,然后当我滚动到下一页时,video
继续播放。当我来到第三页时视频停止。是否有任何编码可以在我滚动到下一页时停止video
或活动?渴望专家帮助!
以下是我的MainActivity,其中包含viewPager和FragmentPageAdapter
public class MainActivity extends AppCompatActivity {
ArrayList<Item> arrayList;
List<Fragment> fragmentList;
MyAdapter adapter;
MyPagerAdapter adapterViewPager;
int passPosition = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
passPosition = getIntent().getIntExtra("key",-1);
arrayList = new ArrayList<>();
adapter = new MyAdapter(this,arrayList);
fragmentList = addItem();
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager(),fragmentList);
vpPager.setAdapter(adapterViewPager);
vpPager.setPageTransformer(true, new CubeOutTransformer());
if(passPosition != -1){
vpPager.setCurrentItem(passPosition);
}
}
private List<Fragment> addItem(){
fragmentList = new ArrayList<>();
arrayList.add(new Item("a",0,0));
fragmentList.add(Fragment1.newInstance("a"));
arrayList.add(new Item("b",0,0));
fragmentList.add(Fragment1.newInstance("b"));
arrayList.add(new Item("c",0,0));
fragmentList.add(Fragment1.newInstance("c"));
arrayList.add(new Item("d",0,0));
fragmentList.add(Fragment1.newInstance("d"));
Toast.makeText(MainActivity.this,"add successfully",Toast.LENGTH_SHORT).show();
return fragmentList;
}
public class MyPagerAdapter extends FragmentStatePagerAdapter {
List<Fragment> fragmentList;
public MyPagerAdapter(FragmentManager fragmentManager,List<Fragment> fragmentList) {
super(fragmentManager);
this.fragmentList = fragmentList;
}
// Returns total number of pages
@Override
public int getCount() {
return this.fragmentList.size();
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
Fragment fm;
fm = this.fragmentList.get(position);
return fm;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
// Returns the page title for the top indicator
@Override
public CharSequence getPageTitle(int position) {
return "Pag " + position;
}
}
}
2.Below是我的Fragment1,其中有videoView
public class Fragment1 extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
private VideoView videoView;
public static final Fragment1 newInstance(String message)
{
Fragment1 f = new Fragment1();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
// Store instance variables based on arguments passed
/*@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = getArguments().getString("someInt");
//title = getArguments().getString("someTitle");
}*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
String message = getArguments().getString(EXTRA_MESSAGE);
TextView textView = (TextView)view.findViewById(R.id.text);
videoView = (VideoView) view.findViewById(R.id.video_view);
textView.setText(message);
videoView.setVideoURI(Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.video));
videoView.stopPlayback();
getVideo();
return view;
}
public void getVideo(){
videoView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
videoView.start();
return true;
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
videoView.start();
}
});
}
}
Picture1 : Video played in first page
Picture2 : when i swipe to 2nd page, the video is still playing and visible! Why???