I have an app that looks something like this:
This is a recyclerview and within the recyclerview are fragments.
The recyclerview xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:id="@+id/main_recyclerview_holder">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
The fragment layout xml that sits within each line of the recyclerview called backgroundRlMaps is displayed below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/backgroundRlMaps"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BACK"/>
<other views...>
</RelativeLayout>
The view you see is first overlayed with a simple view with one button and once the user has clicked on the button, the view displays to reveal the backgroundRlMaps view which shows the map. So for example, each item in the recyclerview would be a different country (china, india, pakistan etc) and each time you click on the map button, the fragment would show the applicable map.
I have pasted the logic below for the mapbutton:
((ViewHolder) holder).mapButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//bringing the fragment container called backgroundRlMaps to the front
((ViewHolder) holder).backgroundRlMaps.bringToFront();
//Dealing with maps here
//A new supportMapFragment is formed every time
SupportMapFragment mapFragment = SupportMapFragment.newInstance();
mapFragment.getMapAsync(RecyclerViewAdapter.this);
//Adding the map to the relativeLayout fragment container
f.getChildFragmentManager().beginTransaction().add(((ViewHolder) holder).backgroundRlMaps.getId(), mapFragment, String.valueOf(position)).commit();
f.getChildFragmentManager().executePendingTransactions();
}
}
However, each time I click on the map button, only the top fragment backgroundRlMaps shows the maps and I can see that the top fragment refreshes its view each time I click on the mapbutton of the other countries. The other two, as in the screenshow, continues to shows nothing.
Is there anyway to get this to work properly?
I suspect that as backgroundRlMaps is duplicated in all my fragments, when I ask the childFragmentManager to add on the SupportMapFragment, it selects the first fragment with that layout reference to add the supportMapFragment to.