This might be a long one:
I have two fragments, one includes Google Maps and the other one is just an ordinary fragment (called InfoFragment) filled with TextViews.
On TextView I want to do next:
1. change from InfoFragment to Map fragment (via Tabs)
2. send textView ID to Map fragment
3. zoom on Marker with the same ID
(Markers are stored in a HashMap)
I already implemented onClickListener (in InfoFragment):
linLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(), "Clicked" + id, Toast.LENGTH_LONG).show();
TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.sliding_tabs);
tabLayout.getTabAt(0).select();
}
});
and it is working great, tabs are switched and the right ID is stored in a variable.
Now, I want to send this ID to Map fragment, check the HashMap, chose the Marker with the same ID and zoom on it. I know how to zoom on it, but I do not know how to catch data?
If I use singleton class (which would store the ID), where in the Map fragment should I catch the data?
EDIT: If I call the method zoomOnTextViewClick():
private void zoomOnTextViewClick(final GoogleMap mMap) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this.getContext());
textViewID = sp.getInt("text_id", -1);
if (textViewID != -1) {
MarkerOptions mark = hash.get(textViewID);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mark.getPosition(), 8));
}
//on app restart
SharedPreferences.Editor editor = sp.edit();
editor.putInt("text_id", -1);
editor.apply();
In onCreateView like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.map_view, container, false);
mView = (MapView) rootView.findViewById(R.id.mapView);
mView.onCreate(savedInstanceState);
mView.onResume(); // needed to get the map to display immediately
mView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
//some other stuff happening also
zoomOnTextViewClick(mMap);
}
});
return rootView;
}
It will work for the first time (when I run the app)
If I call same method in onResume() like this:
@Override
public void onResume() {
super.onResume();
mView.onResume();
mView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
zoomOnTextViewClick(mMap);
}
});
}
Nothing happens.
EDIT #2:
private void setTabs() {
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new FragPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
This is the code which initializes my tabs.