如何隐藏地图片段 - Android

时间:2015-02-03 11:20:05

标签: android google-maps-android-api-2 android-fragmentactivity mapfragment

将以下代码用于showhide MapFragment ,效果很好:

public class MapFragmentActivity extends FragmentActivity {
...........
mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map));
googleMap = mMapFragment.getMap();  
googleMap.setMyLocationEnabled(true);
.....
if(isChecked)
  {                                 
        mMapFragment.getView().setVisibility(View.VISIBLE);                                     
  }
  else 
  {
        mMapFragment.getView().setVisibility(View.GONE);
  }

但无论何时我使用Animation,Map都不会隐藏,它总是visible,而动画对我有效;

if(isChecked)
    {               
        mMapFragment.getView().setVisibility(View.VISIBLE);
        mMapFragment.getView().startAnimation(AnimationUtils.loadAnimation(MapFragmentActivity.this,
         R.anim.slide_up));
    }
    else 
    {
        mMapFragment.getView().setVisibility(View.GONE);
        mMapFragment.getView().startAnimation(AnimationUtils.loadAnimation(MapFragmentActivity.this,
         R.anim.slide_down));
    }

4 个答案:

答案 0 :(得分:5)

可以使用交易显示/隐藏碎片。

try {
        FragmentTransaction ft = .getFragmentManager ().beginTransaction ();
        ft.hide (mMapFragment);
    }
    catch (Exception e) {
        e.printStackTrace ();
    }

答案 1 :(得分:5)

你可以试试这个

      private GoogleMap mMap;
    private SupportMapFragment mMapFragment;

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

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

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

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

}

或者简单的方法是:

    if(isCheked) {
getSupportFragmentManager().beginTransaction().show(mFragment).commit();

        }
        else {
          getSupportFragmentManager().beginTransaction().hide(mFragment).commit();
        }

尝试一下,让我知道它是否有效。

答案 2 :(得分:2)

唯一的方法对我有用:

View fragmentMap = layout.findViewById(R.id.map_fragment_container_id);
fragmentMap.setVisibility(View.GONE);

答案 3 :(得分:0)

您可以将地图片段包装在FrameLayout

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

然后在FrameLayout上调用动画

int valueInPixels = (int) getResources().getDimension(R.dimen.task_list_map_height);
FrameLayout mapLayout = (FrameLayout) findViewById(R.id.mapLayout);
mapLayout.animate().translationY(-valueInPixels).setDuration(600);