1
我對在Android Studio中的viewpager一些問題,添加一個OpenStreetMap的在viewpager
所以我使用viewpager和PagerAdapter類 公共類MainActivity擴展FragmentActivity {
private MapView myOpenMapView;
private MapController myMapController;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager_layout);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
expListView = (ExpandableListView) findViewById(R.id.expandableListView2);
prepareListData();
listAdapter = new ExpandableListAdapter(this,listDataHeader,listDataChild);
}
public class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
public Object instantiateItem(ViewGroup collection, int position) {
View view=null;
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.activity_friend;
view = inflater.inflate(resId, null);
break;
case 1:
resId = R.layout.fragment_main_page;
view = inflater.inflate(resId, null);
break;
case 2:
resId = R.layout.activity_map;
view = inflater.inflate(resId, null);
myOpenMapView = (MapView) findViewById(R.id.openmapview);
//myOpenMapView.setBuiltInZoomControls(true);
//myMapController = myOpenMapView.getController();
//myMapController.setZoom(4);
//myOpenMapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
break;
}
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
//public boolean isViewFromObject(ViewGroup arg0, Object arg1) {
// return arg0 == ((View) arg1);
//}
@Override
public Parcelable saveState() {
return null;
}
}
取得3個畫面的viewpager
這一切正常,現在在我的項目,我想實現一個OpenStreetMap的, 我加入這個代碼
myOpenMapView = (MapView) findViewById(R.id.openmapview);
myOpenMapView.setBuiltInZoomControls(true);
myMapController = myOpenMapView.getController();
myMapController.setZoom(4);
myOpenMapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
這段代碼工作正常,如果它被放在一個單獨的活動,但由於某種原因,如果它是在viewpager oncreate內或頁面內部類它不工作
任何人都可以解釋爲什麼它不工作,以及如何我可以修復它?