我正在使用两个ViewPager
创建VideoViews
。在滑动页面上,当前页面上的视频开始播放。一个问题:如何在屏幕中央的VideoView中显示视频?
这是我的代码,
public class MainActivity extends Activity{
private class MyViewPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener
{
private Vector<View> pages;
private ViewGroup myContainer;
private int currentPageId;
private boolean flag;
public MyViewPagerAdapter(Context context, Vector<View> pages)
{
this.pages=pages;
}
@Override
public int getCount()
{
return pages.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
flag = true;
currentPageId = 0;
int positionNow = myPager.getCurrentItem();
View page = pages.get(position);
container.addView(page);
myContainer = container;
if(positionNow == position)
StartVideo(container, positionNow);
return page;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((View) object);
myContainer = container;
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view.equals(object);
}
@Override
public void onPageScrollStateChanged(int arg0)
{
if(flag)
{
MyVideoView currVideo = (MyVideoView) myContainer.getChildAt(currentPageId+1);
if(currVideo != null)
if(currVideo.getVideoPath() != null)
currVideo.pause();
}
else
flag = !flag;
}
@Override
public void onPageSelected(int arg0)
{
flag = false;
currentPageId = myPager.getCurrentItem();
StartVideo(myContainer, myPager.getCurrentItem());
}
private void StartVideo(ViewGroup container, int position)
{
View page = container.getChildAt(position+1);
MyVideoView curVideo = (MyVideoView) page;
Log.d("Andrew", curVideo.getVideoPath());
curVideo.start();
}
}
private ViewPager myPager;
private MyViewPagerAdapter myAdapter;
private Vector<View> pages;
private MyVideoView myVideoView;
int currentPage, previousPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pages = new Vector<View>();
for(int i=1; i <= 2; i++)
{
myVideoView = new MyVideoView(this);
myVideoView.setVideoPath("videoPath");
pages.add(myVideoView);
}
this.myAdapter = new MyViewPagerAdapter(this, pages);
this.myPager = (ViewPager) this.findViewById(R.id.viewpageronecard);
this.myPager.setAdapter(myAdapter);
myPager.setOnPageChangeListener(myAdapter);
myPager.setOffscreenPageLimit(1);
myPager.setCurrentItem(0);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpageronecard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#222222" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
答案 0 :(得分:0)
虽然这是一个老问题,也许其他人也在努力解决这个问题,因为我遇到了同样的问题。我通过改变
解决了这个问题 android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
到
android:layout_gravity="center"
用于填充ViewPager的VideoView的xml中的。