Noa Drach
Noa Drach

Reputation: 2481

Android - Adding MapFragment inside RecyclerViewAdapter fails if the view isn't visible

Update

my usecase is solved by placing the MapFragment in the xml directly and not adding it later - but I'm keeping the question open because it is still not clear to me why adding it programmatically fails.

Original Question

I have a list of CardViews that are the data inside RecyclerViewAdapter.

In one of my cards I'm adding SupportMapFragment into a FrameLayout with the id map_container in the layout that was inflated for the card.

map_card.xml

<merge
    android:id="@+id/map_card"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="2sp"
    card_view:cardUseCompatPadding="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">
        <!-- Root layout for the card content that is created dynamically -->
        <LinearLayout
            android:id="@+id/card_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <FrameLayout
                android:id="@+id/map_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </LinearLayout>
</merge>

this is how my adapter works

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    FrameLayout frame = new FrameLayout(parent.getContext());
    frame.setLayoutParams(new RecyclerView.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    return new ViewHolder(frame);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    ViewGroup container = (ViewGroup) viewHolder.itemView;
    container.removeAllViews();

    CardView cardView = cards.get(position);
    ViewGroup parent = (ViewGroup) cardView.getParent();
    if (parent != null) {
        parent.removeView(cardView);
    }
    container.addView(cardView);
    ((CustomCard)cardView).onCardBind(fragment);
}

I'm obtaining the MapFragment using

GoogleMapOptions options = new GoogleMapOptions();
SupportMapFragment mapFragment = SupportMapFragment.newInstance(options);

and then I need to add the MapFragment to the card...

If the card is visible on the screen from the beginning - i.e., it's the 1st or 2nd item in the list - doing

FragmentManager fragmentManager = fragment.getChildFragmentManager();
fragmentManager.beginTransaction().add(R.id.map_container, mapFragment).commitAllowingStateLoss();

works!!

but if the card becomes the 3rd item and so initially invisible the same code fails because the fragment manager can't find map_container

ava.lang.IllegalArgumentException: No view found for id 0x7f0d018d (com.myapp.android:id/map_container) for fragment MapFragment{2b012f2b #0 id=0x7f0d018d}
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
       at android.app.BackStackRecord.run(BackStackRecord.java:833)
       at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454)
       at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)

I tried moving the code to the card OnLayout method, calling it in the adapter onBindViewHolder - before adding the view, after adding the view - and I always get the same exception - How can I make it work?

Upvotes: 3

Views: 1049

Answers (1)

Laxminarayan Nayak
Laxminarayan Nayak

Reputation: 215

you should destroy the View before closing it

fm = fragment.getChildFragmentManager();
mMapFragment = (SupportMapFragment) fm.findFragmentById(map.getId());
fm.beginTransaction().remove(mMapFragment).commitAllowingStateLoss();
container.removeView(map);

Upvotes: 1

Related Questions