Charaf
Charaf

Reputation: 23

Baseadapter slow and lag in scrolling

I am trying to make a listview with baseAdapter that show's an ImageView's with text view for every item (Image and Text for every item). However, the listview scrolling is too laggy and slow.

class MyListAdapter:BaseAdapter {
var ListLocal = ArrayList<Food>()
var mCtx : Context?=null
constructor(List:ArrayList<Food>,mCtx:Context){
    this.ListLocal = List
    this.mCtx = mCtx
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): 
View {
    var convertView = convertView
    var holder: ViewHolder

    if (convertView == null) {
        holder= ViewHolder()

        var inflator = LayoutInflater.from(mCtx)
        convertView  = inflator.inflate(R.layout.food_type,null)
        holder.title= convertView.findViewById(R.id.txtT)
        holder.img = convertView.findViewById(R.id.PicT)

        convertView.tag = holder
    }
    else {
        holder = convertView.tag as ViewHolder

    }
    val item = ListLocal[position]
    holder.title!!.text = item.txtname
    holder.img!!.setImageResource(item.picc!!)
    return convertView!!

}

override fun getItem(position: Int): Any {
    return ListLocal[position]
}

override fun getItemId(position: Int): Long {
    return position.toLong()
}

override fun getCount(): Int {
    return ListLocal.size
}




 }

internal class ViewHolder {
    var title: TextView? = null
    var img: ImageView? = null
}

Upvotes: 1

Views: 128

Answers (1)

Eric Bachhuber
Eric Bachhuber

Reputation: 3952

If I had to guess, this line is the cause of your problems: holder.img!!.setImageResource(item.picc!!)

Because you are having performance issues, you should use an image loader like Glide or Picasso rather than directly calling setImageResource. The size of your Bitmaps/Drawables can severely degrade scrolling performance unless they are very small as they aren't cached and will be decoded at 1:1 size rather than to the size of the target ImageView. However, it's impossible to tell if that is your actual performance issue without seeing the more code/image assets.

Upvotes: 1

Related Questions