0
我的主要活動佈局包含一個ViewPager。 這個ViewPager有不同的頁面,它們通過一組XML佈局文件被誇大。ViewPager中的VideoView
我有一個XML佈局文件,裏面有一個VideoView。 如何初始化此VideoView?
這是活動的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;
}
我認爲應該在這裏初始化?
你是否嘗試過這樣,VideoView mVideoView =(VideoView)pager.findViewById(R.id.myvideoview); –
無法正常工作。我認爲這是由於佈局尚未膨脹的問題,因此無法找到VideoView。 – humansg