27

xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mapFragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>

通常のフラグメントでは、次のようになります。

  mFragment = (mFragment) (getSupportFragmentManager().findFragmentById(R.id.mFragment));
  mFragment.getView().setVisibility(View.INVISIBLE);

Googleマップフラグメント:

  mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment)).getMap();

しかし、マップフラグメントの可視性を段階的に設定するにはどうすればよいですか?

他の人の断片のようにそれを行うことはできません。

4

5 に答える 5

65

このように簡単です:

private GoogleMap mMap;
private SupportMapFragment mMapFragment;

mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
mMap = mMapFragment.getMap();

mMapFragment.getView().setVisibility(View.INVISIBLE);
于 2012-12-12T20:18:19.890 に答える
22

できるよ:

getSupportFragmentManager().beginTransaction().hide(mFragment).commit();

それを表示するには:

getSupportFragmentManager().beginTransaction().show(mFragment).commit();
于 2012-12-20T13:40:42.213 に答える
1

答えはどちらも正しいですが@David@ferdy182文脈を教えてくれません。

プログラムでフラグメントを非表示/表示する@ferdy182場合は、xmlにあるフラグメントを非表示/表示する場合に使用します。あなたは従うべきです@David

説明させてください

xmlに単一のframeLayoutがあり、その特定のフラグメントの他のフラグメントを次々に置き換えたい場合。このコードを使用して、すべてのフラグメントを追加します。それらは互いに配置されます。

for(int i=0;i<4;i++)
        {
            getFragmentManager().beginTransaction().add(R.id.container, frag[i])
            //.addToBackStack(null)
            .commit();
        }// add all these fragment and put them on each other then 

表示したいものを除いて、他のすべてのフラグメントを非表示にします。

for(int j=0;j<4;j++)
        {
        getFragmentManager().beginTransaction().hide(frag[j]).commit();
        }
        getFragmentManager().beginTransaction().show(frag[0]).commit();

利点 これらのフラグメントは、C#のフォームのように機能します。Forum.showおよびforum.hide(); 。現在の状態が残っているところ。これらのフラグメントは何度も呼び出されません。ここでの問題は、この手法を使用して解決します。2番目の方法

xmlに複数のframeLayoutまたはfragmentがある場合。IDを取得することで、その特定のものを非表示にできます。

private GoogleMap mMap;
private SupportMapFragment mMapFragment;

mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
mMap = mMapFragment.getMap();

mMapFragment.getView().setVisibility(View.INVISIBLE);

コード

//非表示のときにフラグメントを表示します

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
          .show(fragment1)
          .commit();

//フラグメントを非表示にします

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
          .hide(fragment1)
          .commit();
于 2014-07-17T07:37:24.497 に答える
0

それだけが私のために働きます:

View fragmentMap = layout.findViewById(R.id.map_fragment_container_id);
fragmentMap.setVisibility(View.GONE);
于 2016-06-02T17:46:40.077 に答える
0

in xmlを作成する代わりに、SupportMapFragmentin xmlのコンテナを定義しSupportMapFragment、クラスからマップをロードする別のアプローチが考えられます。

XMLでは、コンテナはFrameLayout-

    <FrameLayout
        android:id="@+id/mapContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

である私のJavaクラスでは、Fragmentマップを表示および非表示にする2つのメソッドを作成しました。デタッチを使用しましたが、必要に応じて異なります。デタッチの代わりに非表示を使用することもできます。

   private void showMap() {
   mMapContainer.setVisibility(View.VISIBLE);
   mSupportMapFragment = SupportMapFragment.newInstance();
   getChildFragmentManager().beginTransaction()
                         .replace(R.id.mapContainer, mSupportMapFragment).commit();
   }

    private void hideMap() {
        mMapContainer.setVisibility(View.VISIBLE);
        if (mSupportMapFragment != null) {
            getChildFragmentManager().beginTransaction()
                                 .detach(mSupportMapFragment).commit();
        }
    }
于 2017-04-08T18:53:54.793 に答える