图形页面没有从ViewPager删除? [英] MapView not being removed from ViewPager?

查看:74
本文介绍了图形页面没有从ViewPager删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想实现一个ViewPager内的地图视图。我在申请4不同的页面。和图形页面是在第4页。我没有成功加载地图,但是当我回到轻扫到第一页第四视图必须使用destroyItem()方法来销毁。如果我刷到第4页,从第3页崩溃显示在logcat的一个错误:

  05-10 13:14:50.152:E / AndroidRuntime(620):java.lang.IllegalStateException:你只允许有一个图形页面中MapActivity

林知道有只在MapActivity一个MapView类的。但我没有能够解决吗? - 谁能帮助我

code是如下:

 公共对象instantiateItem(查看收集,INT位置){   LayoutInflater吹气=(LayoutInflater)collection.getContext()
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   上下文= collection.getContext();
   INT渣油= 0;
   INT标志=渣油;
   开关(位置){
   情况下0:
      渣油= R.layout.farleft; //第一
      鉴于= inflater.inflate(渣油,NULL);      打破;
   情况1:
      渣油= R.layout.left; //第二
      鉴于= inflater.inflate(渣油,NULL);      打破;
   案例2:
      渣油= R.layout.right; //第三
      鉴于= inflater.inflate(渣油,NULL);      打破;
   案例3:
      渣油= R.layout.mylocator;
      鉴于= inflater.inflate(渣油,NULL);
      打破;   }
   ((ViewPager)集合).addView(查看,0);   返回视图。
}@覆盖
公共无效destroyItem(查看为arg0,ARG1 INT,对象ARG2){   ((ViewPager)为arg0).removeView((查看)ARG2);
   Log.d(destroyItem,+ ARG2);
}


解决方案

我已经找到了解决办法。
里面的片段,它包含的地图和所使用的ViewPager,端起

 私有视图MyView的;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){    如果(this.myView == NULL){
        this.myView = inflater.inflate(R.layout.my_layout,集装箱,FALSE);
    }
    否则,如果(this.myView.getParent()!= NULL){
        的FrameLayout parentView =(的FrameLayout)this.myView.getParent();
        parentView.removeView(this.myView);
    }    返回this.myView;}

它工作正常的我,告诉我它是否适合你;)

In my application I'm trying to implement a map view inside a ViewPager. I have 4 different pages in the application. and MapView is in the 4th page. I did successfully load the map, but when I swipe back to the first page the 4th view must be destroyed using destroyItem() method. And if I swipe to the 4th page, it crashes from 3rd page showing an error in the 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

Im aware of having only a single mapview in a MapActivity. But I'm not able to solve it - can anyone help me?

Code is 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);
}

解决方案

I have found the solution. Inside your fragment which contains the Map and that is used by the ViewPager, put that :

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;

}

It works fine for me, tell me if it works for you ;)

这篇关于图形页面没有从ViewPager删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆