gpt4 book ai didi

android - 在 viewpager 中添加 openstreetmap

转载 作者:行者123 更新时间:2023-11-30 02:20:09 24 4
gpt4 key购买 nike

我对android studio中的viewpager有一些疑问,

所以我使用 viewpager 和 PagerAdapter 类制作了一个具有 3 个屏幕的 viewpager公共(public)类 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 类中,则无法正常工作

谁能解释为什么它不起作用以及我该如何解决它?

最佳答案

                    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

View map = v.findViewById(R.id.openmapview); 

就这样但是你是从膨胀 View 中创建的

然后你把刚刚获取的id填入map中

myOpenMapView = (MapView)map;

希望对遇到同样问题的人有所帮助

关于android - 在 viewpager 中添加 openstreetmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28679656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com