0

I've seen a few similar questions, but couldn't solve my problem. I have a ListView with one ImageView and TextView. I'm using custom adapter and viewHolder pattern but my list still doesn't scroll smooth? I have already downloaded images in my mipmap folder, so I am not downloading them directly. I don't know what else can I do to improve fast scroll, like in Quora or any other professional ListView app? Here is my adapter:

class customAdapter extends BaseAdapter {

ArrayList<Object> itemList;
Activity context;
public LayoutInflater inflater;

public customAdapter(Activity context,ArrayList<Object> itemList) {
    super();

    this.context = context;
    this.itemList = itemList;
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

private static class ViewHolder {
    TextView text;
    ImageView image;
}

@Override
public int getCount() {
    return itemList.size();
}

@Override
public Object getItem(int position) {
    return itemList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){

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

        convertView = inflater.inflate(R.layout.custom_row, parent, false);
        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.image = (ImageView) convertView.findViewById(R.id.image);
        convertView.setTag(holder);
    } else
        // View is being recycled, retrieve the viewHolder object from tag
        holder = (ViewHolder) convertView.getTag();

        ListItem items = (ListItem) itemList.get(position);
        holder.text.setText(items.getText());
        holder.image.setImageResource(items.getImg());

        return convertView;
    }
}

Here is my custom_row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/image"
    android:layout_marginTop="25dp"
    android:layout_marginLeft="20dp"
    android:src="@mipmap/austria" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/text"
    android:layout_marginTop="45dp"
    android:layout_marginLeft="30dp"
    android:textColor="#ffffff"
    android:textStyle="bold" />

And here is the sample of pictures I'm using: http://img.freeflagicons.com/thumb/round_icon/austria/austria_640.png

Pegasus Thorn
  • 159
  • 1
  • 2
  • 12
  • Why do you use `ListView` if there is `RecyclerView` for a while? Also show please post your `custom_row.xml` and sample of images which are you using here. – Divers Jan 04 '17 at 14:37
  • I'm relative new to Android, and trying to develop my first app. So I thought ListView is a good choice. – Pegasus Thorn Jan 04 '17 at 14:44
  • Hmm.. I don't see any problem int the adapter side. May be you should be more specific on how you are fetching the listview data( not only image of course) and populating it – Abhisek Lamsal Jan 04 '17 at 14:44
  • @PegasusThorn please show what I've asked for. – Divers Jan 04 '17 at 14:45
  • @Divers I've added what you've asked for. I change image in every row, of course. Just another country flag. – Pegasus Thorn Jan 04 '17 at 14:50
  • @PegasusThorn it doesn't matter if you using RecycleView or ListView. If it really matter listView should be deprecated as early as the RecycleView debut. – teck wei Jan 04 '17 at 14:51
  • @teckwei It does matter, because RecyclerView does a lot of performance optimization. And even if in current case problem not in RecyclerView, if you'd like to be good developer, you should move forward with progress, otherwise you will die as dinosaurs did. – Divers Jan 04 '17 at 14:56
  • @Divers I know, you are very true. But currently his problem doesn't solve even switch to RecycleView. – teck wei Jan 04 '17 at 14:58
  • @Divers yes RecyclerView does a lot more performance improvements. But I don't think simply suggesting the alternative is what he needs. He wants to know what is causing the problem so it would be better if the issue is identified and solved. – Abhisek Lamsal Jan 04 '17 at 15:00
  • @PegasusThorn try loading the image in a separate thread and check if it helps – Abhisek Lamsal Jan 04 '17 at 15:03
  • @AbhisekLamsal I was asking, not suggesting. – Divers Jan 04 '17 at 15:04
  • Even I'm new to Android, I'm not new to a programming, and I know how important optimization is. So I checked many websites on scrolling smoothly, and most of them suggested ViewHolder, and I implemented it, but still didn't get flying list. Maybe it is a size of the pictures, indeed. – Pegasus Thorn Jan 04 '17 at 15:05
  • @PegasusThorn as it's already suggested, your problem in huge images and in that you load them on main thread. So you can just use normal size images and use `Picasso` or `Glide` libraries in order to process them in worker threads. – Divers Jan 04 '17 at 15:06
  • @PegasusThorn why don't you load image in the ImageView like this: http://stackoverflow.com/questions/11086129/runonuithread-inside-a-view – Abhisek Lamsal Jan 04 '17 at 15:09
  • @Divers If I my ask, what should be appropriate size for an image in ListView, if I have just one image and some text next to it? – Pegasus Thorn Jan 04 '17 at 15:10
  • @PegasusThorn I assume it should be 70dp x 70dp. – Divers Jan 04 '17 at 15:12
  • @Divers So, even I put 70dp x 70dp in my xml file, it still needs to resize them in the main thread, which cause my list not to run smoothly? I should use Picasso to resize them in another thread and then use them? Did I get it right? – Pegasus Thorn Jan 04 '17 at 15:15
  • @PegasusThorn for best result yes. – Divers Jan 04 '17 at 15:16

1 Answers1

2

Check the size of the images. Loading bigger sized images impact listview performance.

Kadari
  • 311
  • 1
  • 3
  • 5