【问题标题】:How to Hide Map Fragment - Android如何隐藏地图片段 - Android
【发布时间】:2015-04-02 13:46:53
【问题描述】:

使用下面的代码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 一起使用时,地图永远不会隐藏,它总是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));
    }

【问题讨论】:

  • 如果我的回答解决了您的问题,请告诉我,否则我会更新它...但您必须接受任何正确的答案! :)
  • 检查我的答案,它可能会有所帮助 - stackoverflow.com/a/43299111/1380032
  • 你解决了吗?为了帮助其他人,如果这有助于您随时投票并将其标记为正确答案。谢谢:)

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


【解决方案1】:

你可以试试这个

      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();
        }

试试这个,让我知道它是否有效。

这是 Kotlin 中第一种方法的 sn-p...

val mapFragment = supportFragmentManager
                .findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.view?.visibility = View.GONE

【讨论】:

    【解决方案2】:

    可以使用事务显示/隐藏片段。

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

    【讨论】:

      【解决方案3】:

      唯一对我有用的方法:

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

      【讨论】:

        【解决方案4】:

        您可以将地图片段包装在 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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-26
          • 2013-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-06
          • 2017-09-09
          • 2011-10-27
          相关资源
          最近更新 更多