Search code examples
androidlistlistviewandroid-asynctask

Make ListView Scroll Smoothly


I have the following code to load my ListView (Each Item Has an Image and Text):

public View getView(final int i, View convertView, ViewGroup viewGroup) {
    View view = inflater.inflate(R.layout.contact_inflater, null);
    final Holder holder = new ContactAdapter.Holder();

    holder.ivPhoto = (ImageView) view.findViewById(R.id.ivImage);
    holder.tvFullName = (TextView) view.findViewById(R.id.tvName);
    holder.tvPhoneNumber = (TextView) view.findViewById(R.id.tvPhoneNo);
    holder.tvFullName.setText(Contact.get(i).get(0));
    holder.tvPhoneNumber.setText(Contact.get(i).get(1));
    holder.button = (Button) view.findViewById(R.id.ivButton);
        
    Picasso.with(context)
                .load(Contact.get(i).get(2))
                .into(holder.ivPhoto);
    
    return view;
}

The problem is the list isn't scrolling smoothly. How do I make it smooth?

EDIT: I have changed to RecycleView but I forgot to add the following code:

if(//IF//){ 
    Log.d("InInIn!", "InInIn!"); 
    
    //Turn to Tagged
}

When I turn it into a RecycleView, The button turns Tagged all over the pace and the tagged buttons change as a scroll. The buttons are changing all over the place even though they shouldn't. Whats the issue?


Solution

  • You should use recyclerView instead. It is a more advanced and flexible version of ListView. Go through https://developer.android.com/guide/topics/ui/layout/recyclerview for guide.

    If you want to stick with ListView then in order to improve the performance you should recycle/reuse the views that are no longer visible to the user.

    public View getView(final int i, View convertView, ViewGroup viewGroup) {
           
        Holder holder = null;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.contact_inflater, null);
            final Holder holder = new ContactAdapter.Holder();
            holder.ivPhoto = (ImageView) convertView.findViewById(R.id.ivImage);
            holder.tvFullName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvPhoneNumber = (TextView) convertView.findViewById(R.id.tvPhoneNo);
            holder.button = (Button) convertView.findViewById(R.id.ivButton);
            convertView.setTag(holder)
        }
        else{
            holder = (Holder)convertView.getTag()
        } 
    
        holder.tvFullName.setText(Contact.get(i).get(0));
        holder.tvPhoneNumber.setText(Contact.get(i).get(1));
    
        Picasso.with(context)
                    .load(Contact.get(i).get(2))
                    .into(holder.ivPhoto);
    
        return convertView;
    }