Android 将 recyclerview 的最后一项移到广告上方

Android move last item of recyclerview above ad

这就是我想要的。

recyclerview 的最后一项应该始终在广告上方。现在它被广告部分隐藏了。应用程序由 main_layout 组成,其中 FrameLayout 的名称为 content_frame 片段所在的位置。这是我用代码写的:

activity_main.xml

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

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/app_bar_layout"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigation"
        android:animateLayoutChanges="true"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorBottomNavigationViewBg"
        app:itemIconTint="?attr/colorBottomNavigationViewItem"
        app:itemTextAppearanceActive="@color/colorAccent"
        app:itemTextColor="?attr/colorBottomNavigationViewItem"
        app:labelVisibilityMode="selected"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/bottom_navigation_items" />

</androidx.constraintlayout.widget.ConstraintLayout >

fragment_list.xml

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

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        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" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>

FragmentList.java

private void initAds(View view) {
    RelativeLayout relativeLayout = view.findViewById(R.id.rl);
    AdView adView = new AdView(view.getContext());
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    layoutParams.addRule(RelativeLayout.BELOW, view.findViewById(R.id.recyclerview).getId());
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    adView.setLayoutParams(layoutParams);
    adView.setAdUnitId(BuildConfig.FLAVOR.equals("dev") ? AppConstants.TEST_BANNER_ID : AppConstants.FRAGMENT_LIST_BANNER_ID);
    adView.setAdSize(AdSize.BANNER);
    relativeLayout.addView(adView);
    adView.setId(Util.getIdNotUsed(requireActivity()));
    adView.loadAd(new AdRequest.Builder().build());

    RecyclerView rv = view.findViewById(R.id.recyclerview);
    RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams2.addRule(RelativeLayout.ABOVE, adView.getId());
    rv.setLayoutParams(layoutParams2);
}

那么如何让回收站视图的最后一项位于广告上方呢?如果您还需要什么,请告诉我。谢谢。

您可以添加广告视图并在广告未显示时将其可见性设置为“GONE”,并在显示广告时使其可见并将 recyclerview 限制在其顶部。

我发现了一个问题。我应该放 SwipeRefreshLayout 而不是 RecyclerView rv = view.findViewById(R.id.recyclerview);添加以上内容。