MainActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.gms.ads.*
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import com.neco_desarrollo.earnapp.adapters.CategoryAdapter
import com.neco_desarrollo.earnapp.adapters.ContentManager
import com.neco_desarrollo.earnapp.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var adapter: CategoryAdapter? = null
private var interAd: InterstitialAd? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initAdMob()
(application as AppMainState).showAdIfAvailable(this){}
initRcView()
}
private fun initRcView() = with(binding){
adapter = CategoryAdapter()
rcViewCat.layoutManager = LinearLayoutManager(
this@MainActivity,
LinearLayoutManager.HORIZONTAL,
false)
rcViewCat.adapter = adapter
adapter?.submitList(ContentManager.list)
}
override fun onResume() {
super.onResume()
binding.adView.resume()
loadInterAd()
}
override fun onPause() {
super.onPause()
binding.adView.pause()
}
override fun onDestroy() {
super.onDestroy()
binding.adView.destroy()
}
private fun initAdMob(){
MobileAds.initialize(this)
val adRequest = AdRequest.Builder().build()
binding.adView.loadAd(adRequest)
}
private fun loadInterAd(){
val adRequest = AdRequest.Builder().build()
InterstitialAd.load(this,
"ca-app-pub-3940256099942544/1033173712", adRequest,
object : InterstitialAdLoadCallback(){
override fun onAdFailedToLoad(p0: LoadAdError) {
interAd = null
}
override fun onAdLoaded(ad: InterstitialAd) {
interAd = ad
}
})
}
private fun showInterAd(){
if(interAd != null){
interAd?.fullScreenContentCallback =
object : FullScreenContentCallback(){
override fun onAdDismissedFullScreenContent() {
showContent()
interAd = null
loadInterAd()
}
override fun onAdFailedToShowFullScreenContent(p0: AdError) {
showContent()
interAd = null
loadInterAd()
}
override fun onAdShowedFullScreenContent() {
interAd = null
loadInterAd()
}
}
interAd?.show(this)
} else {
showContent()
}
}
private fun showContent(){
Toast.makeText(this, "Запуск контента", Toast.LENGTH_LONG).show()
}
}
CategoryAdapter
import android.content.res.ColorStateList
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.neco_desarrollo.earnapp.R
import com.neco_desarrollo.earnapp.databinding.CategoryItemBinding
class CategoryAdapter : ListAdapter<String, CategoryAdapter.Holder>(Comparator()) {
class Holder(view: View) : RecyclerView.ViewHolder(view) {
private val binding = CategoryItemBinding.bind(view)
fun setData(text: String) = with(binding){
tvCatTitle.text = text
cardViewCat.backgroundTintList = ColorStateList
.valueOf(Color.parseColor(ContentManager.colorList[adapterPosition]))
}
}
class Comparator : DiffUtil.ItemCallback<String>(){
override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
return oldItem == newItem
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.category_item, parent, false)
return Holder(view)
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.setData(getItem(position))
}
}
ContentManager
object ContentManager {
val list = arrayListOf(
"Фразы философов",
"Предсказания",
"Мотивация",
"Любовь",
"Слово дня"
)
val colorList = arrayListOf(
"#F61010",
"#1092F6",
"#6124D1",
"#D1A024",
"#30D124"
)
}
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcViewCat"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/adView" />
</androidx.constraintlayout.widget.ConstraintLayout>
category_item.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:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="@+id/cardViewCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:backgroundTint="#DF1E1E"
app:cardCornerRadius="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:gravity="center"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/tvCatTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/white"
android:textSize="16sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>