如何获取 ViewPager 当前视图,这是在 onPageSelected 中向用户显示的视图
How to get ViewPager's current View which is the one show to user in onPageSelected
我用这种方式获取ViewPager的当前View :
private class OuterAdapter extends PagerAdapter{
private SwipingVideoPage currentPage;
@Override
public Object instantiateItem(ViewGroup container, int position) {
SwipingVideoPage page = new SwipingVideoPage();
container.addView(page);
return page;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
currentPage = (SwipingVideoPage)object;
super.setPrimaryItem(container, position, object);
}
public SwipingVideoPage getCurrenPage() {
return currentPage;
}
}
我想我可以调用 ViewPager.getAdapter.getCurrentPage()
来获取当前视图。但我发现它在 ViewPager.OnPageChangeListener.onPageSelected()
:
中不起作用
private class OuterChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageSelected(int position) {
((OuterAdapter)outerViewPager.getAdapter()).getCurrenPage() //this returns the previous shown view, not the right one
}
}
也许 onPageSelected()
在 setPrimaryItem()
之前被调用,这是正确的吗?
以及如何在 onPageSelected()
中获取 ViewPage
的主视图?
在最新的软件包中你也可以使用
vp.getCurrentItem()
或
vp is the viewPager ,
pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);
您必须为您的 viewPager 放置一个页面更改侦听器。 viewPager 上没有获取当前页面的方法。
private int currentPage;
private static class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
Log.i(TAG, "page selected " + position);
currentPage = position;
}
}
正如@Mohammad Fareed所说,我在ViewPager
中没有找到直接获取当前页面的方法。
所以我用这种方式来解决问题:
private Map<Integer, View> pageMap; //records all the pages in the ViewPager
private SwipingVideoPage currentPage;
private class OuterAdapter extends PagerAdapter{
@Override
public Object instantiateItem(ViewGroup container, int position) {
SwipingVideoPage page = new SwipingVideoPage();
container.addView(page);
pageMap.put(position, page); //add page
return page;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
pageMap.remove(position); //remove page which is removed from ViewPager
}
}
private class OuterChangeListener extends ViewPager.SimpleOnPageChangeListener {
@Override
public void onPageSelected(int position) {
currentPage = pageMap.get(position); //get current page
}
}
我在我的项目中使用了这些代码,到现在为止效果很好。
我用这种方式获取ViewPager的当前View :
private class OuterAdapter extends PagerAdapter{
private SwipingVideoPage currentPage;
@Override
public Object instantiateItem(ViewGroup container, int position) {
SwipingVideoPage page = new SwipingVideoPage();
container.addView(page);
return page;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
currentPage = (SwipingVideoPage)object;
super.setPrimaryItem(container, position, object);
}
public SwipingVideoPage getCurrenPage() {
return currentPage;
}
}
我想我可以调用 ViewPager.getAdapter.getCurrentPage()
来获取当前视图。但我发现它在 ViewPager.OnPageChangeListener.onPageSelected()
:
private class OuterChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageSelected(int position) {
((OuterAdapter)outerViewPager.getAdapter()).getCurrenPage() //this returns the previous shown view, not the right one
}
}
也许 onPageSelected()
在 setPrimaryItem()
之前被调用,这是正确的吗?
以及如何在 onPageSelected()
中获取 ViewPage
的主视图?
在最新的软件包中你也可以使用
vp.getCurrentItem()
或
vp is the viewPager ,
pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);
您必须为您的 viewPager 放置一个页面更改侦听器。 viewPager 上没有获取当前页面的方法。
private int currentPage;
private static class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
Log.i(TAG, "page selected " + position);
currentPage = position;
}
}
正如@Mohammad Fareed所说,我在ViewPager
中没有找到直接获取当前页面的方法。
所以我用这种方式来解决问题:
private Map<Integer, View> pageMap; //records all the pages in the ViewPager
private SwipingVideoPage currentPage;
private class OuterAdapter extends PagerAdapter{
@Override
public Object instantiateItem(ViewGroup container, int position) {
SwipingVideoPage page = new SwipingVideoPage();
container.addView(page);
pageMap.put(position, page); //add page
return page;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
pageMap.remove(position); //remove page which is removed from ViewPager
}
}
private class OuterChangeListener extends ViewPager.SimpleOnPageChangeListener {
@Override
public void onPageSelected(int position) {
currentPage = pageMap.get(position); //get current page
}
}
我在我的项目中使用了这些代码,到现在为止效果很好。