1

I have an app that looks something like this:

app

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.

Simon
  • 19,658
  • 27
  • 149
  • 217

1 Answers1

-1

The problem is that each item in your RecyclerView is Fragment. It is not possible to put Fragment as ListView or RecyclerView item.

See more information here

Community
  • 1
  • 1
Oleksandr
  • 6,226
  • 1
  • 46
  • 54
  • I'm not sure that would work as that would just change the base view from the recyclerview to a listview but the fragments is still going to be in each line of the listview and so i would get back to the same problem. – Simon Sep 25 '15 at 11:47
  • @Simon The main thing in my answer that it is not possible to put Fragment as ListView item! – Oleksandr Sep 25 '15 at 12:27