4

の 2 ページ目にあるGoogleMapからオブジェクトを取得しようとしています。その内容は で与えられます。FragmentViewPagerFragmentPagerAdapter

ViewPager通常のレイアウトと Google マップの 2 ページで構成されています。

これを行う最初のレイアウトにアクセスできます。

View tempView = LayoutInflater.from(getApplication())
                .inflate(R.layout.start_activity, null);
tempTextView = (TextView) tempView.findViewById(R.id.timer);

ここではすべて問題ありません。しかし、Google マップにもアクセスしたいのです。
マップが別のアクティビティにあった場合、次のようにしてGoogleMapオブジェクトを取得できました。

map=((SupportMapFragment)getSupportFragmentManager()
    .findFragmentById(R.id.map)).getMap();

しかし今、上記の行は戻りますnull

問題は、フラグメントからオブジェクトを取得する方法GoogleMapです。

彼らはこれを行うことを提案しています(これが機能すると言う人もいます)が、私には効果がありません:

map = ((SupportMapFragment) getSupportFragmentManager()
      .findFragmentByTag("android:switcher:" + R.id.pager + ":1")).getMap();

ここでコードを提供しています。

4

2 に答える 2

7

ここで解決策を見つけましたが、他のユーザーがコードを見て、私のように何週間も行き詰まらないように、ここにも投稿します。

MyPagerAdapter.java

public class MyPagerAdapter extends FragmentPagerAdapter
{
    private Context context;
    final LatLng    YEREVAN = new LatLng(40.181, 44.513);

    public MyPagerAdapter(FragmentManager fm, Context context)
    {
        super(fm);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position)
    {
        switch (position)
        {
            case 0:
                return new MyFragment();
            case 1:
                return MyMapFragment.newInstance(YEREVAN);
        }
        return null;
    }

    @Override
    public int getCount()
    {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position)
    {
        Locale l = Locale.getDefault();
        switch (position)
        {
            case 0:
                return context.getString(R.string.start).toUpperCase(l);
            case 1:
                return context.getString(R.string.map).toUpperCase(l);
        }
        return null;
    }
}

MyMapFragment.java

public class MyMapFragment extends SupportMapFragment
{
    private LatLng  latLon;

    public MyMapFragment()
    {
        super();
    }

    public static MyMapFragment newInstance(LatLng position)
    {
        MyMapFragment frag = new MyMapFragment();
        frag.latLon = position;
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = super.onCreateView(inflater, container, savedInstanceState);
        initMap();
        return v;
    }

    private void initMap()
    {
        UiSettings settings = getMap().getUiSettings();
        settings.setAllGesturesEnabled(false);
        settings.setMyLocationButtonEnabled(false);

        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLon, 16));
        getMap().addMarker(new MarkerOptions().position(latLon).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
    }
}
于 2013-03-21T13:14:12.917 に答える
1

問題は、 s をMyFragmentPagerAdapter使用せず、SupportMapFragments のみを追加することMyFragmentです。getItemその機能を次のように変更する必要があります。

public Fragment getItem(int position) {
    Fragment fragment;
    switch (position) {
    case 0:
        fragment = new MyFragment();
        break;
    case 1:
        fragment = new MySupportMapFragment();
        break;
    }
    Bundle args = new Bundle();
    args.putInt(MyFragment.ARG_SECTION_NUMBER, position);
    fragment.setArguments(args);
    return fragment;
}

そのためにはMySupportMapFragment、 で行ったのと同様に、レイアウトをロードする独自のタイプを実装する必要がありますMyFragment

その後、SupportMapFragment を から直接呼び出すことができますFragmentPagerAdapter。お気に入り:

SupportMapFragment supportMapFragment = 
    (SupportMapFragment) mMyFragmentPagerAdapter.getItem(1);
map = supportMapFragment.getMap();

アダプターに追加すると、期待する SupportMapFragment を取得する必要があります。

于 2013-03-17T16:40:50.903 に答える