如何在android kotlin中加载2行数据后设置adview

How to set adview after 2 row set of data loading in android kotlin

提问人:chanthini begam 提问时间:6/9/2023 最后编辑:chanthini begam 更新时间:6/9/2023 访问量:15

问:

如何在每加载 2 组图像后设置 adview。一组 3 行图像被添加到回收器视图中。每 2 组图片后,广告必须在嵌套滚动视图中显示。Kotlin 代码、xml 和 adview xml 如下

private fun loadNext27UI(peopleNearbyFirstRowList: ArrayList<PeopleNearByModelClass>,peopleNearbySecondRowList: ArrayList<PeopleNearByModelClass>,peopleNearbyThirdRowList: ArrayList<PeopleNearByModelClass>) {
    if (activity != null) {
        try {
            val value: Int = minPeopleCountDummy!!
            val value2: Int? = allPeoples?.size
            if (value != (value2?. minus(1))) {
                if (value != (value2?. minus(2))) {
                    coroutineScope.launch(Dispatchers.IO) {
                        if (peopleNearbyFirstRowList.size > 0) {                            
                            for (i in peopleNearbyFirstRowList.indices) {                                   
                                val viewFirst: View = LayoutInflater.from(contxt).inflate(R.layout.list_item_right_adapter, null)
                                val imageViewFirst = viewFirst.findViewById<ImageView>(R.id.imageGeneric)
                                val textViewFirst = viewFirst.findViewById<TextView>(R.id.textViewGeneric)
                                val image: String = peopleNearbyFirstRowList[i].userImage
                                val userId1: Int =peopleNearbyFirstRowList[i].userID                                  

                                if (image.isNotEmpty()) {
                                    try {
                                        Glide.with(this@PeopleNearByFragment).asBitmap().load(image).circleCrop()
                                            .into(BitmapImageViewTarget(imageViewFirst))
                                    } catch (e: Exception) {
                                        e.printStackTrace()
                                    }
                                } else {
                                    Glide.get(requireActivity()).clearDiskCache()
                                }
                                textViewFirst.text = peopleNearbyFirstRowList[i].userName                                  
                                withContext(Dispatchers.Main) {
                                    peopleNearbyMainLay1?.addView(viewFirst)                                     
                                }
                            }
                        }
                        if (peopleNearbySecondRowList.size > 0) {                           

                            for (j in peopleNearbySecondRowList.indices) {
                                @SuppressLint("InflateParams")
                                val viewSecond: View =     LayoutInflater.from(contxt)
                                    .inflate(R.layout.list_item_right_adapter, null)
                                val imageViewSecond =
                                    viewSecond.findViewById<ImageView>(R.id.imageGeneric)
                                val textViewSecond =
                                    viewSecond.findViewById<TextView>(R.id.textViewGeneric)
                                val imageSecond = peopleNearbySecondRowList[j].userImage
                                val userIdSecond: Int = peopleNearbySecondRowList[j].userID
                                if (imageSecond.isNotEmpty()) {
                                    try {
                                        Glide.with(this@PeopleNearByFragment).asBitmap().load(imageSecond)
                                            .circleCrop()
                                            .into(BitmapImageViewTarget(imageViewSecond))
                                    } catch (e: Exception) {
                                        e.printStackTrace()
                                    }
                                } else {
                                    Glide.get(requireActivity()).clearDiskCache()
                                }
                                textViewSecond.text = peopleNearbySecondRowList[j].userName
                                withContext(Dispatchers.Main) {
                                    peopleNearbyMainLay2?.addView(viewSecond)                                      
                                }
                            }
                        }
                        if (peopleNearbyThirdRowList.size > 0) {                               
                            for (k in peopleNearbyThirdRowList.indices) {
                                @SuppressLint("InflateParams")
                                val viewThird: View = LayoutInflater.from( contxt).inflate(R.layout.list_item_right_adapter, null)
                                val imageViewThird = viewThird.findViewById<ImageView>(R.id.imageGeneric)
                                val textViewThird = viewThird.findViewById<TextView>(R.id.textViewGeneric)
                                val imageThird: String = peopleNearbyThirdRowList[k].userImage
                                val userIdThird: Int = peopleNearbyThirdRowList[k].userID
                                if (imageThird.isNotEmpty()) {
                                    try {
                                        Glide.with(this@PeopleNearByFragment).asBitmap().load(imageThird)
                                            .circleCrop()
                                            .into(BitmapImageViewTarget(imageViewThird))
                                    } catch (e: Exception) {
                                        e.printStackTrace()
                                    }
                                } else {
                                    Glide.get(requireActivity()).clearDiskCache()
                                }
                                textViewThird.text = peopleNearbyThirdRowList[k].userName
                                withContext(Dispatchers.Main) {
                                    peopleNearbyMainLay3?.addView(viewThird)                                   
                                }
                            }
                        }
                        nestedScrollView?.isNestedScrollingEnabled = true                          
                    }
                }
            }

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




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 += peoples[i]
                    peopleNearbySecondRowList+= peoples[i. plus(1)]
                    peopleNearbyThirdRowList += peoples[i. plus(2)]
                    peopleNearbyFirstRow += peoples[i]
                    peopleNearbySecondRow += peoples[i. plus(1)]
                    peopleNearbyThirdRow += peoples[i. plus(2)]
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            loadNext27UI(
                peopleNearbyFirstRowList,
                peopleNearbySecondRowList,
                peopleNearbyThirdRowList                   
            )                          }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

XML格式:

            <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>

adview xml:这是我应该在 for 循环之间添加的 adview。

<LinearLayout
    android:id="@+id/ad_lay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:visibility="visible"
    android:orientation="horizontal"
    tools:ignore="MissingConstraints">

    <application.activity.TemplateView
        android:id="@+id/my_template"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:gnt_template_type="@layout/gnt_small_template_view" />

</LinearLayout>

enter image description here

人造人 列表 科特林 设置 AdView的

评论


答: 暂无答案