我的主要活动布局由 ViewPager 组成。这个 ViewPager 中有各种页面,它们通过一组 XML 布局文件进行扩展。
我拥有的一个 XML 布局文件中有一个 VideoView。如何在其中初始化此 VideoView?
这是Activity的onCreate方法。我无法初始化 VideoView,因为它没有被 ViewPagerAdapter 充气。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewPagerAdapter adapter = new ViewPagerAdapter(this);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
pager.setAdapter(adapter);
indicator.setViewPager(pager);
// mVideoView is null.. where should I init this?
VideoView mVideoView = (VideoView) findViewById(R.id.myvideoview);
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.start();
}
这是 ViewPagerAdapter 中的 instantiateItem 方法。
public Object instantiateItem( View pager, int position )
{
//Inflate the correct layout
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//layouts[] is an int[] that points to resources such as R.layout.start_page
View inflatedView = layoutInflater.inflate(layouts[position], null);
((ViewPager)pager).addView(inflatedView,0);
// is this the correct way to do this?
if (position == 1) {
VideoView mVideoView = (VideoView) inflatedView.findViewById(R.id.myvideoview);
mVideoView.setVideoPath(path);
mVideoView.start();
}
return inflatedView;
}
我认为它应该在这里初始化?