blongho / worldCountryData

Android library for country flag, currency, and other country information
https://blongho.github.io/worldCountryData/doc/
MIT License
98 stars 35 forks source link

Skipping frames when using in recycler view #12

Closed moeindev closed 4 years ago

moeindev commented 4 years ago

When I use this library in recycler view adapter, my app will run slow!

*Smartphone

blongho commented 4 years ago

I am not sure if this is due to the library or not. How is the implementation of your recycler view? How do you populate the list?

moeindev commented 4 years ago

Hello, here is my adapter: ` import android.content.Context import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import coil.api.load import com.blongho.country_data.World import ir.moeindeveloper.coronameter.R import ir.moeindeveloper.coronameter.core.data.pojo.summary.Country import ir.moeindeveloper.coronameter.databinding.ItemCountrySummeryBinding import ir.moeindeveloper.coronameter.ui.viewUtils.CountryClickListener import ir.moeindeveloper.coronameter.util.extensions.toStringNumber

class CountryAdapter(private val listener: CountryClickListener,private val context: Context): RecyclerView.Adapter<CountryAdapter.CountryViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountryViewHolder {
    val binding = ItemCountrySummeryBinding.inflate(LayoutInflater.from(parent.context),parent,false)
    return CountryViewHolder(binding)
}

override fun getItemCount(): Int = countries.size

override fun onBindViewHolder(holder: CountryViewHolder, position: Int) {
    holder.bindData(countries[position],listener,context)
}

private var countries: List<Country> = listOf()

fun updateData(countries: List<Country>){
    if (this.countries != countries) {
        this.countries = countries
        notifyDataSetChanged()
    }
}

class CountryViewHolder(private val binding: ItemCountrySummeryBinding): RecyclerView.ViewHolder(binding.root) {
    fun bindData(country: Country,listener: CountryClickListener,context: Context) {
        binding.root.setOnClickListener {
            listener.onCountrySelected(country)
        }
        binding.countryName.text = country.country

        binding.flagImage.load(World.getFlagOf(country.countryCode))
        binding.itemTotalConfirmed.text = "${context.resources.getString(R.string.item_total_confirmed)}   ${country.totalConfirmed.toStringNumber()}"
    }
}

}`

I reduce the frames by using coil library, but when it goes to another activity(no matter what, even an empty one) and when it comes back to the first page where the recycler view is, skipping frames will happen

I use Architecture components and MVVM architecture

blongho commented 4 years ago

I do not experience the same from this sample application

https://play.google.com/store/apps/details?id=com.blongho.countrydata

👆 uses MVVM with fragments and 1 activity

moeindev commented 4 years ago

turned out the problem was third image view lib causing the problem, Thank u and I'm closing this