MapView not removed from ViewPager?

In my application, I am trying to implement a map view inside a ViewPager. I have 4 different pages in the application. and MapView is on page 4. I successfully loaded the map, but when I return to the first page, the fourth view must be destroyed using the destroyItem () method. And if I scroll to the 4th page, it crashes from the third page shows an error in logcat:

05-10 13:14:50.152: E/AndroidRuntime(620): java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity

I know that MapActivity has only one mapping. But I can’t solve it - can someone help me?

Code below:

public Object instantiateItem(View collection, int position) {

   LayoutInflater inflater = (LayoutInflater) collection.getContext()
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   context = collection.getContext();
   int resId = 0;
   int flag = resId;
   switch (position) {
   case 0:
      resId = R.layout.farleft; // first
      view = inflater.inflate(resId, null);

      break;
   case 1:
      resId = R.layout.left; // second
      view = inflater.inflate(resId, null);

      break;
   case 2:
      resId = R.layout.right; // third
      view = inflater.inflate(resId, null);

      break;
   case 3:
      resId = R.layout.mylocator;
      view = inflater.inflate(resId, null);
      break;

   }
   ((ViewPager) collection).addView(view, 0);

   return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {

   ((ViewPager) arg0).removeView((View) arg2);
   Log.d("destroyItem", "" + arg2);
}
+3
source share
2 answers

I have found a solution. Inside your fragment that contains the map and that is used by ViewPager, put this:

private View myView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    if (this.myView == null){
        this.myView = inflater.inflate(R.layout.my_layout, container, false);
    }
    else if (this.myView.getParent() != null) {
        FrameLayout parentView = (FrameLayout) this.myView.getParent();
        parentView.removeView(this.myView);
    }

    return this.myView;

}

, , ;)

+1

Android View Pager , . , , , . , instantiateItem, .

0

All Articles