MainActivity
Код: Выделить всё
package com.example.restart
import android.content.ContentValues.TAG
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import com.example.restart.api.ApiRequest
import com.example.restart.api.CatalogAdapter
import com.example.restart.api.CatalogModel
import com.example.restart.databinding.ActivityMainBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.Retrofit
import retrofit2.awaitResponse
import retrofit2.converter.gson.GsonConverterFactory
class MainActivity : AppCompatActivity() {
private var binding: ActivityMainBinding? = null
private var adapterCatalog = CatalogAdapter()
lateinit var catalogList: ArrayList
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding!!.root)
getData()
}
private lateinit var allblock: List
private fun initCategory(category: List) {
with(binding!!){
listCatalogInMain.layoutManager = GridLayoutManager(this@MainActivity, 1)
listCatalogInMain.adapter = adapterCatalog
var cataloglist: List = category
if(cataloglist.isNotEmpty()){
for(element in cataloglist){
adapterCatalog.addCatalog(element)
}
}
}
}
private fun getData(){
val api = Retrofit.Builder()
.baseUrl("https://medic.madskill.ru")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiRequest::class.java)
CoroutineScope(Dispatchers.IO).launch {
try {
val response = api.getCatalog().awaitResponse()
if (response.isSuccessful) {
val data = response.body()!!
catalogList = data as ArrayList
runOnUiThread { initCategory(catalogList) }
Log.d(TAG, data.toString())
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
Log.d(TAG, e.toString())
}
}
}
}
}
Код: Выделить всё
import retrofit2.Call
import retrofit2.http.GET
interface ApiRequest {
@GET("/api/catalog")
fun getCatalog(): Call
}
Код: Выделить всё
package com.example.restart.api
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.restart.R
import com.example.restart.databinding.ItemBlockBinding
class CatalogAdapter: RecyclerView.Adapter() {
private val listCatalog = ArrayList()
class CategoryHolder(item: View): RecyclerView.ViewHolder(item){
private var bindingCategory = ItemBlockBinding.bind(item)
fun bind(category: CatalogModel){
bindingCategory.name.text = category.name
bindingCategory.category.text = category.category
bindingCategory.bio.text = category.bio
bindingCategory.price.text = category.price
bindingCategory.timeResult.text = category.time_result
bindingCategory.preparation.text = category.preparation
bindingCategory.description.text = category.description
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_block, parent, false)
return CategoryHolder(view)
}
override fun getItemCount(): Int {
return listCatalog.size
}
override fun onBindViewHolder(holder: CategoryHolder, position: Int) {
holder.bind(listCatalog[position])
}
fun addCatalog(category: CatalogModel){
listCatalog.add(category)
notifyDataSetChanged()
}
}
Код: Выделить всё
Код: Выделить всё
.....
.....
Подробнее здесь: https://stackoverflow.com/questions/759 ... ing-layout
Мобильная версия