How to set adview after every 27 users of image loading in android kotlin

24 Views Asked by At

How to set adview after every 27 sets of user images loaded. After every sets of images(27users) loaded small ad must be show into the nested scroll view. Kotlin code and adview xml are as follows:

    private fun loadNext27(peoples: List<PeopleNearByModelClass>) {

    try {

        coroutineScope.launch(Dispatchers.IO) {

            minPeopleCount =
                peopleNearbyFirstRow.size + peopleNearbySecondRow.size + peopleNearbyThirdRow.size
            val maxPeopleCount = peoples.size.coerceAtMost(minPeopleCount!! + 27)

            minPeopleCountDummy = maxPeopleCount

            peopleNearbyFirstRowList = ArrayList()
            peopleNearbySecondRowList = ArrayList()
            peopleNearbyThirdRowList = ArrayList()

            var i: Int = minPeopleCount as Int
            try {
                while (i < maxPeopleCount) {
                    i += 3
                    peopleNearbyFirstRowList.add(peoples[i])
                    peopleNearbySecondRowList.add(peoples[i + 1])
                    peopleNearbyThirdRowList.add(peoples[i + 2])

                    peopleNearbyFirstRow.add(peoples[i])
                    peopleNearbySecondRow.add(peoples[i + 1])
                    peopleNearbyThirdRow.add(peoples[i + 2])
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            loadNext27UI(
                peopleNearbyFirstRowList,
                peopleNearbySecondRowList,
                peopleNearbyThirdRowList, peoples.size
            )
        }

    } catch (e: Exception) {
        e.printStackTrace()
    }
}

I want to set this adview code into this method.

private fun showAd() {

}

the following code is the xml code where i want to add the adview into the nested scroll view:

  <LinearLayout
        android:id="@+id/containerPeopleNearby"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:visibility="visible"
        android:layout_weight="1"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/peopleNearbyNestedScroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:paddingBottom="36dp"
            android:layout_margin="0dp"
            android:scrollbars="none">
            <LinearLayout
                android:id="@+id/linearLayoutDynamic"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:baselineAligned="false"
                android:orientation="horizontal"
                tools:ignore="UnusedAttribute">
                <LinearLayout
                    android:id="@+id/peopleNearbyMainLay1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:clipToPadding="false"
                    android:nestedScrollingEnabled="false"
                    android:orientation="vertical"
                    android:overScrollMode="never"
                    android:paddingTop="40dp"
                    android:scrollbars="none"
                    tools:listitem="@layout/list_item_right_adapter" />
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">
                    <com.google.android.material.card.MaterialCardView
                        android:id="@+id/people_nearby_user_image_layout"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_centerInParent="false"
                        android:layout_centerHorizontal="true"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="5dp"
                        android:visibility="gone"
                        app:cardCornerRadius="25dp"
                        app:cardElevation="8dp">

                        <com.google.android.material.imageview.ShapeableImageView
                            android:id="@+id/people_nearby_user_image"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_gravity="center"
                            android:layout_marginStart="8dp"
                            android:layout_marginTop="8dp"
                            android:layout_marginEnd="8dp"
                            android:layout_marginBottom="8dp"
                            android:scaleType="fitXY"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            tools:ignore="ContentDescription"/>

                    </com.google.android.material.card.MaterialCardView>

                    <LinearLayout
                        android:id="@+id/peopleNearbyMainLay2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:clipToPadding="false"
                        android:nestedScrollingEnabled="false"
                        android:orientation="vertical"
                        android:overScrollMode="never"
                        android:paddingTop="95dp"
                        android:scrollbars="none"
                        tools:listitem="@layout/list_item_right_adapter"/>

                </RelativeLayout>

                <LinearLayout
                    android:id="@+id/peopleNearbyMainLay3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:clipToPadding="false"
                    android:nestedScrollingEnabled="false"
                    android:orientation="vertical"
                    android:overScrollMode="never"
                    android:paddingTop="40dp"
                    android:scrollbars="none"
                    tools:listitem="@layout/list_item_right_adapter" />


                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/ad_lay"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/linearLayoutDynamic"

                    android:layout_gravity="center|bottom"
                    android:layout_marginBottom="10dp"
                    android:gravity="center"
                    android:orientation="horizontal"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/linearLayoutDynamic"
                    app:layout_constraintVertical_bias="1.0"
                    app:layout_constraintVertical_chainStyle="spread_inside">


            </LinearLayout>


        </androidx.core.widget.NestedScrollView>


    </LinearLayout>
0

There are 0 best solutions below