Call another fragment page when clicking google ma

2019-08-11 06:56发布

I am using retrofit to fetch data online.However I cannot find any solution to my problem. I want to call another fragment and display more of the details about the marker I clicked and pass those values to another fragment. Can someone please help me on this. Any help and solutions are well appreciated.

MapFragment.jav

 RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(getString(R.string.fine_dinings))
            .build();

    RestaurantPlacesApiInterface restaurantPlacesApiInterface =
            adapter.create(RestaurantPlacesApiInterface.class);

    restaurantPlacesApiInterface.getStreams(new Callback<List<RestaurantPlaces>>() {
        @Override
        public void success(List<RestaurantPlaces> restaurantPlaces, Response response) {
            for (RestaurantPlaces restaurantPlace : restaurantPlaces){
                MarkerOptions options = new MarkerOptions().position(new LatLng(restaurantPlace.getLatitude(),
                        restaurantPlace.getLongitude()));
                options.title(restaurantPlace.getName());
                options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                getMap().addMarker(options);
            }
        }

        @Override
        public void failure(RetrofitError error) {

        }
    });

}

2条回答
Melony?
2楼-- · 2019-08-11 07:44

You need to use this:

@Override
public boolean onMarkerClick(Marker marker) {
    // call fragment and pass data.
    return false; 
}

If you return false the click is not consumed. If you need help implementing this let me know, it's fairly simple.

Here is a quick sample, please change the names to match your own code:

public class MapActivity implements OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
    private GoogleMap mGoogleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        initMap();
    }

    public void initMap() {
        MapFragment map = (MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment);
        map.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        try {
            if (googleMap != null) {
                mGoogleMap = googleMap;
                mGoogleMap.setOnMarkerClickListener(this);              
                mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                // Now make your retrofit call
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("ERROR", "GOOGLE MAPS NOT LOADED");
        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        Bundle bundle = new Bundle();
        bundle.putString("myString", "value");
        // set Fragment class Arguments
        MyFragment myFragment= new MyFragment();
        myFragment.setArguments(bundle);
        // launch fragment

        return false; 
    }
}
查看更多
We Are One
3楼-- · 2019-08-11 07:47

better to use interface in fragment and implement that interface in (activity where RestAdapter used)

查看更多
登录 后发表回答