Android Jetpack

Retrofit + ViewModelScope + RecyclerView + Glide Example

----___<<<<< 2022. 4. 20. 01:06

 

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1")

implementation 'com.github.bumptech.glide:glide:4.13.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'

 

class CustomAdapter(val context : Context, val dataSet: List<Plant>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    class ViewHolder(view : View) : RecyclerView.ViewHolder(view) {

        val textView : TextView = view.findViewById(R.id.textArea)
        val imageView : ImageView = view.findViewById(R.id.imageArea)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.text_row_item, parent, false)

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.textView.text = dataSet[position].name
        Glide.with(context)
            .load(dataSet[position].imageUrl)
            .into(holder.imageView)


    }

    override fun getItemCount(): Int {
        return dataSet.size
    }


}

 

interface MyApi {

    @GET("googlecodelabs/kotlin-coroutines/master/advanced-coroutines-codelab/sunflower/src/main/assets/plants.json")
    suspend fun getAllPlants() : List<Plant>

}

 

object RetrofitInstance {

    val BASE_URL = "https://raw.githubusercontent.com/"

    val client = Retrofit
        .Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    fun getInstance() : Retrofit {
        return client
    }

}

 

data class Plant (
    val plantId: String,
    val name: String,
    val description: String,
    val growZoneNumber: Int,
    val wateringInterval: Int,
    val imageUrl: String
)

 

class MainViewModel : ViewModel() {

    var retrofitInstance : MyApi = RetrofitInstance.getInstance().create(MyApi::class.java)

    private val _result = MutableLiveData<List<Plant>>()
    val result: LiveData<List<Plant>>
        get() = _result

    fun test() = viewModelScope.launch {

        Log.d("MainViewModel", retrofitInstance.getAllPlants().toString())
        _result.value = retrofitInstance.getAllPlants()

    }

}

 

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val viewModel  = ViewModelProvider(this).get(MainViewModel::class.java)

        val rv = findViewById<RecyclerView>(R.id.rv)


        viewModel.test()
        viewModel.result.observe(this, Observer {
            Log.d("MainActivity", it.toString())

            val customAdapter = CustomAdapter(this, it)
            rv.adapter = customAdapter
            rv.layoutManager = LinearLayoutManager(this)

        })

    }
}

'Android Jetpack' 카테고리의 다른 글

WorkManager 고유작업  (0) 2022.05.15
WorkManager 주기적 실행  (0) 2022.05.02
Retrofit Simple Example RecyclerView 추가  (0) 2022.04.18
기존 예제 변경  (0) 2022.04.17
Simple Coroutine / ViewModelScope  (0) 2022.04.16