Оставьте AdView закрепленным в верхней части при прокрутке RecyclerView - PullRequest
2 голосов
/ 11 июля 2019

У меня есть RecyclerView и выше этого, есть AdView.При прокрутке RecyclerView я бы хотел оставить Adview в фиксированном положении.Как я могу это сделать?

Вот мой RecyclerView при открытии приложения.

enter image description here

После прокрутки RecyclerView

enter image description here

Я использую FrameLayout, щелкая открытое меню фрагмента, как показано ниже:

frg = (Fragment)
ContainerFragment.newInstance(getString(R.string.DefaultGaleria), getString(R.string.DefaultFrase), getString(R.string.DefaultGif));

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, frg);
transaction.commit();

Вот макет.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adViewMob"
            android:background="@color/transparent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/ad_unit_id"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

Моя активность_основная

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:fitsSystemWindows="true"
    android:animateLayoutChanges="true"
    >
<!-- android:layout_above="@+id/linear"-->


    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/coordinator"
        >

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"

            android:fitsSystemWindows="true"
            >

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways" />

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways"
                app:tabMode="fixed"

                />

        </com.google.android.material.appbar.AppBarLayout>



        <!--<include layout="@layout/sample" />
        <include layout="@layout/content_main" />
        -->

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_above="@+id/adViewMob"
            android:animateLayoutChanges="true"
            android:fitsSystemWindows="false"

            />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/activity_main_drawer"
        app:headerLayout="@layout/nav_header_main"

        android:theme="@style/NavigationView"
        app:itemTextColor="#ffffff"
        android:background="#1a1b1d"
        />
    <!--app:itemBackground="@drawable/good" -->

<!--    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />-->

</androidx.drawerlayout.widget.DrawerLayout>

Ответы [ 2 ]

0 голосов
/ 11 июля 2019

вставьте этот код в ваш XML-файл ..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adViewMob"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/transparent"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/ad_unit_id" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_below="@+id/adViewMob"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</RelativeLayout>
0 голосов
/ 11 июля 2019

Вы можете сделать это легко, используя RelativeLayout, как показано ниже.

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingTop="50dp" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adViewMob"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/transparent"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/ad_unit_id" />
</RelativeLayout>

Это должно служить вашей цели.

...