代码之家  ›  专栏  ›  技术社区  ›  alan eugenio

与SupportMapFragment中另一个布局中的按钮交互

  •  1
  • alan eugenio  · 技术社区  · 8 年前

    我想知道在单击地图中的标记时如何隐藏/显示FloatingAction按钮。

    public class MapsActivity extends SupportMapFragment implements 
    OnMapReadyCallback, GoogleMap.OnMapClickListener, 
    GoogleMap.OnMarkerClickListener, GoogleMap.OnInfoWindowClickListener {
    
    private static final String TAG = "Maps";
    private GoogleMap mMap;
    private LocationManager locationManager;
    private ArrayList<LatLng> latlngs = new ArrayList<>();
    private MarkerOptions markerOptions = new MarkerOptions();
    private Marker markerN;
    private Marker markerO;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMapAsync(this);
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
    
        try {
            Criteria criteria = new Criteria();
            locationManager = (LocationManager) 
    getActivity().getSystemService(Context.LOCATION_SERVICE);
            mMap = googleMap;
            mMap.setOnMapClickListener(this);
            mMap.setOnMarkerClickListener(this);
            mMap.getUiSettings().setZoomControlsEnabled(true);
            mMap.getUiSettings().setMapToolbarEnabled(false);
            mMap.setMyLocationEnabled(true);
            Location location = 
    locationManager.getLastKnownLocation(locationManager
                    .getBestProvider(criteria, false));
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            LatLng sydney = new LatLng(latitude, longitude);
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 15.0f));
            mMap.setOnInfoWindowClickListener(this);
        } catch (SecurityException ex) {
            Log.e(TAG, "Error", ex);
            requestPermissions(new String[]
    {Manifest.permission.ACCESS_FINE_LOCATION},
                    1);
        } catch (Exception e) {
            System.out.println(e);
        }
    
        try {
            // Customise the styling of the base map using a JSON object defined
            // in a raw resource file.
            boolean success = mMap.setMapStyle(
                    MapStyleOptions.loadRawResourceStyle(
                            getContext(), R.raw.style_json));
    
            if (!success) {
                Log.e("MapsActivityRaw", "Style parsing failed.");
            }
        } catch (Resources.NotFoundException e) {
            Log.e("MapsActivityRaw", "Can't find style.", e);
        }
    
        latlngs.add(new LatLng(-22.978608, -49.869901));
        for (LatLng point : latlngs) {
            markerOptions.position(point);
            markerOptions.title("Local");
            markerOptions.snippet("Info");
            markerO = mMap.addMarker(markerOptions);
        }
    }
    
    @Override
    public void onMapClick(LatLng latLng) {
        mMap.clear();
        markerOptions.position(latLng);
    
        markerOptions.title("Deseja cadastrar este local?");
        markerOptions.snippet("Pressione aqui");
        markerN = mMap.addMarker(markerOptions);
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
        Toast.makeText(getContext(), "Coord: " + latLng.toString(), 
    Toast.LENGTH_LONG).show();
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String 
    permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
    {
    
                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    
    @Override
    public void onInfoWindowClick(Marker marker) {
        if (marker.equals(markerN)) {
            Toast.makeText(getContext(), "Clique 2", Toast.LENGTH_LONG).show();
        } else {
    
        }
    }
    
    @Override
    public boolean onMarkerClick(Marker marker) {
        if (marker.equals(markerN)) {
            Toast.makeText(getContext(), "Clique 1", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getContext(), "Abrir Informações", 
    Toast.LENGTH_LONG).show();
        }
        return false;
    }
    }
    

    这就是我想要交互的XML:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.alan.unigeo.MainActivity">
    
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    
    </android.support.design.widget.AppBarLayout>
    
    <include layout="@layout/content_main" />
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    我试图寻找类似这种情况的例子,但失败了。 地图位于布局的一个片段内,FloatingActionButton位于前一个布局“之上”的另一个布局中。我不知道我是否需要显示任何其他代码来帮助你们,如果需要的话,只求它。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Andreas Engedal    8 年前

    我的程序中有这个功能。

    如果不想显示,请使用以下代码:

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ((FloatingActionButton) getView().getRootView().findViewById(R.id.fab)).hide();
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ((FloatingActionButton) getView().getRootView().findViewById(R.id.fab)).show();
    }
    

    如果你想展示它,只需切换 .隐藏()