嵌套谷歌地图碎片定制的片断里嵌套、片断、碎片、地图

2023-09-05 04:01:37 作者:世界再无第二个我

我想创建一个片段fragments.One刷卡意见应在主要活动直接换GoogleMap的MapFragment.Inflating MapFragment工作ok.But通过FragmentPagerAdapter做罚球源Choreographer.class。我没有发现找不到知道为什么它happens.I基于设置这一切,这个答案。所以我的code是这样的:

I am trying to create swipe views with fragments.One of the fragments should wrap GoogleMap MapFragment.Inflating MapFragment directly in main activity works ok.But doing via FragmentPagerAdapter throws source not found in Choreographer.class .I can't find a clue why it happens.I set it all up based on this answer .So my code looks like this:

MainActivity

public class MainActivity extends FragmentActivity {

FragPagerAdapter _fragPagerAdapter;
ViewPager _viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    _fragPagerAdapter = new FragPagerAdapter(getFragmentManager());
    _viewPager = (ViewPager)findViewById(R.id.pager);
    _viewPager.setAdapter(_fragPagerAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

public class FragPagerAdapter extends  FragmentPagerAdapter {

    public FragPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = null;

        if(position == 1){
        fragment = new LocMapFragment(); //CRASHES 
        }else{
            //Doesn't contain map .WORKS
            fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
        }

        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:

            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}


}

LocMapFragment

public class LocMapFragment extends Fragment {

private GoogleMap  _map;
@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState){

  View v = inflater.inflate(R.layout.map_layout, null, false);

  _map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
      .getMap();

  return v;
   }
}

map_layout布局

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

    <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

 </RelativeLayout>

我使用的Andr​​oid的API,而不是从支持V4.I片段试图改变片段布局类=com.google.android.gms.maps.SupportMapFragment,仍然是相同的。

I am using Fragments from Android API and not from support V4.I tried to change fragment layout to class="com.google.android.gms.maps.SupportMapFragment" and still the same.

更新:

好了,不知怎的,它开始工作now.But现在发生的事情是,如果地图片段是在第一个刷卡的片段,然后我有两个多刷卡,只需text.If我从地图到另一个滚动,然后滚动回,所以我Choreographer.class变得异常。

Ok,Somehow it started working now.But what happens now is that if the map fragment is in the first swipe fragment and then I have two more swipes with just text.If I scroll from the map to another and then scroll back ,I am getting exception on Choreographer.class.

This答案似乎已经解决了这个问题。

推荐答案

看看这个实现,你应该为你已经这样做,创建一个片段,在这一个图:

Take a look at this implementation, you should as you already did, Create a fragment with a map in it:

public class MapFragment extends Fragment

创建布局: mapview.xml:

create a layout: mapview.xml:

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapview"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="12"
map:mapType="normal"
map:uiZoomControls="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiZoomGestures="true"
map:uiTiltGestures="false" />

然后在片段做到这一点:

then in the fragment do this:

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

    return inflater.inflate(R.layout.mapview, container, false);
}

然后拿到地图的实例:

then to get the map instance:

private GoogleMap getGoogleMap() {
        if (map == null && getActivity() != null && getActivity().getSupportFragmentManager()!= null) {
            SupportMapFragment smf = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview);
            if (smf != null) {
                map = smf.getMap();
            }
        }
        return map;
    }