在 viewpager 中添加 openstreetmap
Adding an openstreetmap in a viewpager
我对 android studio 中的 viewpager 有一些疑问,
所以我使用 viewpager 和 PagerAdapter 制作了一个具有 3 个屏幕的 viewpager class
public class 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;
}
}
这很好用,现在我想在我的项目中实现一个 openstreetmap,
我添加了这段代码
myOpenMapView = (MapView) findViewById(R.id.openmapview);
myOpenMapView.setBuiltInZoomControls(true);
myMapController = myOpenMapView.getController();
myMapController.setZoom(4);
myOpenMapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
如果将此代码放在单独的 activity 中,则此代码可以正常工作,但出于某种原因,如果它位于 viewpager 的 oncreate 或 pageradapter 中 class,则无法正常工作
谁能解释为什么它不起作用以及我该如何解决它?
resId = R.layout.activity_map;
v = inflater.inflate(resId,null);
View map = v.findViewById(R.id.openmapview);
myOpenMapView = (MapView)map;
myOpenMapView.setBuiltInZoomControls(true);
myOpenMapView.setMultiTouchControls(true);
myOpenMapView.setClickable(true);
myMapController = myOpenMapView.getController();
myMapController.setZoom(15);
myOpenMapView.getController().setCenter(
new GeoPoint(MAP_LATITUDE, MAP_LONGITUDE));
myOpenMapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
break;
所以我想通了,
您创建了一个新视图
View map = v.findViewById(R.id.openmapview);
如此
但你是用膨胀视图创建的
然后你把刚刚获取的id填入地图
myOpenMapView = (MapView)map;
希望这可以帮助一些 运行 遇到同样问题的人
我对 android studio 中的 viewpager 有一些疑问,
所以我使用 viewpager 和 PagerAdapter 制作了一个具有 3 个屏幕的 viewpager class public class 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;
}
}
这很好用,现在我想在我的项目中实现一个 openstreetmap, 我添加了这段代码
myOpenMapView = (MapView) findViewById(R.id.openmapview);
myOpenMapView.setBuiltInZoomControls(true);
myMapController = myOpenMapView.getController();
myMapController.setZoom(4);
myOpenMapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
如果将此代码放在单独的 activity 中,则此代码可以正常工作,但出于某种原因,如果它位于 viewpager 的 oncreate 或 pageradapter 中 class,则无法正常工作
谁能解释为什么它不起作用以及我该如何解决它?
resId = R.layout.activity_map;
v = inflater.inflate(resId,null);
View map = v.findViewById(R.id.openmapview);
myOpenMapView = (MapView)map;
myOpenMapView.setBuiltInZoomControls(true);
myOpenMapView.setMultiTouchControls(true);
myOpenMapView.setClickable(true);
myMapController = myOpenMapView.getController();
myMapController.setZoom(15);
myOpenMapView.getController().setCenter(
new GeoPoint(MAP_LATITUDE, MAP_LONGITUDE));
myOpenMapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
break;
所以我想通了,
您创建了一个新视图
View map = v.findViewById(R.id.openmapview);
如此 但你是用膨胀视图创建的
然后你把刚刚获取的id填入地图
myOpenMapView = (MapView)map;
希望这可以帮助一些 运行 遇到同样问题的人