How to avoid admob adview being cut off

2020-05-07 05:44发布

问题:

I have an Activity with two fragments, one that changes on top and another one which is an admob adview that always stays fix at the bottom of the screen. Mysteriously it gets cut off when back button is pressed from the fragment that I show you now:

public class FragmentChallengeFullDecription extends Fragment {

    private Challenge challengeClicked = null;
    private ImageView challengeImage;
    private TextView challengeDescription;

    public FragmentChallengeFullDecription() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_challenge_full_decription, container, false);


        Bundle bundle = getArguments();

        if (bundle != null) {
            challengeClicked = bundle.getParcelable("CHALLENGE_CLICKED");
        }

        CollapsingToolbarLayout collapsingToolbar = view.findViewById(R.id.collapsingToolbar);
        collapsingToolbar.setTitle(challengeClicked.getName());


        challengeImage = view.findViewById(R.id.challengeImage);
        challengeImage.setImageResource(challengeClicked.getImage());


        challengeDescription = view.findViewById(R.id.challengeDescription);
        challengeDescription.setText(challengeClicked.getDescription());


        return view;
    }
}

This is the video showing the mistake: https://imgur.com/gallery/pRFhG9e?s=wa

Do you think the problem may be associated with the CollapsingToolbarLayout, which I've heard it's pretty buggy? I'm lost.

As required, this is the xml file of the last fragment in which the Collapsing Toolbar Layout is contained:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentChallengeFullDecription"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:expandedTitleMarginStart="40dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >

        <ImageView
            android:id="@+id/challengeImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_launcher_background"
            app:layout_collapseMode="parallax"/>          

        <include layout="@layout/toolbar"
            android:id="@+id/myToolbar"/>        

    </android.support.design.widget.CollapsingToolbarLayout>



</android.support.design.widget.AppBarLayout>


<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:elevation="5dp"
    android:scrollbars="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:paddingBottom="?attr/actionBarSize"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:orientation="vertical"
        android:paddingTop="10dp"
        >

        <TextView
            android:id="@+id/challengeDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Description"/>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

This is the xml of the fragment where the adview is contained:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        ads:layout_constraintStart_toStartOf="parent"
        ads:layout_constraintEnd_toEndOf="parent"
        ads:layout_constraintBottom_toBottomOf="parent"
        >
    </com.google.android.gms.ads.AdView>


</FrameLayout>

And this is how I add the adview fragment to the general activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">


    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/bannerFragment"
        >

    </FrameLayout>


    <fragment
        android:id="@+id/bannerFragment"
        android:name="com.example.silentlibrary.FragmentBanner"
        tools:layout="@layout/fragment_banner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />
</android.support.constraint.ConstraintLayout>