如何隐藏地图碎片 - 安卓

7
使用以下代码来显示隐藏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));
    }

如果我的回答解决了你的问题,请告诉我,否则我会更新它...但你必须接受任何答案为正确! :) - Skizo-ozᴉʞS ツ
检查一下我的答案,它可能会有所帮助 - https://dev59.com/c2Yr5IYBdhLWcg3wQH--#43299111 - Rahul
你解决了吗?如果这个回答对你有帮助,请随意点赞并将其标记为正确答案,以便帮助其他人。谢谢 :) - Skizo-ozᴉʞS ツ
4个回答

11

你可以尝试这个

      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 中第一个方法的代码片段...

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

5

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

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

-1

你可以将地图片段包裹在一个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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接