2015-02-23 54 views
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內或頁面內部類它不工作

任何人都可以解釋爲什麼它不工作,以及如何我可以修復它?

回答

1
    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); 

這樣 但你創造出來膨脹的觀點

,然後你填帶您剛剛購買的身份證的地圖

myOpenMapView = (MapView)map; 

希望這可以幫助一些遇到同樣問題的人