Question

I'm developing a new Home Launcher that for the moment use a listview to display installed applications. I'm using a custom adapter to provide the applications name and icon. If i leave the app name i got no issues, however, if i add icons the listview becomes really slow and unusable. I also tried with asynctask with no luck. Here is relevant pices of my adapter:

public AppInfoAdapter(Context c, List list, ArrayList<String> appList, PackageManager pm, Resources resources) {
    mContext = c;
    mListAppInfo = list;
    mPackManager = pm;
    mAppList = appList;
    mResources = resources;
    activity = (Activity) c;
}

=====

   @Override

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

    View v = convertView;

    if(v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.launcher_adapter_row, null);
    }

    if (v != null) {
        mIcon = (ImageView) v.findViewById(R.id.imageView1);
        tvAppName = (TextView) v.findViewById(R.id.textView1);
    }

    new addItems().execute(tvAppName, mIcon, position);

    return v;
}

====

public class addItems extends AsyncTask<Object, String, Void> {

    private TextView title;
    private ImageView icon;
    private int pos;

    @Override
    protected Void doInBackground(final Object... parameters) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                title = (TextView) parameters[0];
                icon = (ImageView) parameters[1];
                pos = (Integer) parameters[2];

                entry = (ApplicationInfo) mListAppInfo.get(pos);

                icon.setImageDrawable(entry.loadIcon(mPackManager));
                title.setText(entry.loadLabel(mPackManager));
            }
        });

        return null;
    }
}

Tried with listview and also with gridview. The result is the same. It's quite impossible to scroll due to stuttering. Any suggestions? Thanks

Was it helpful?

Solution

You are again loading data in the Ui thread in AsynchTask so change your AsynchTask like this..

public class addItems extends AsyncTask<Object, String, Drawable> {

    private TextView title;
    private ImageView icon;
    private int pos;

    @Override
    protected Drawable doInBackground(final Object... parameters) {
        title = (TextView) parameters[0];
        icon = (ImageView) parameters[1];
        pos = (Integer) parameters[2];

        entry = (ApplicationInfo) mListAppInfo.get(pos);

        Drawable drawable = entry.loadIcon(mPackManager);
        return drawable;
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        icon.setImageDrawable(result);
        title.setText(entry.loadLabel(mPackManager));
    }
}

here i am loading drawable in another thread and passing it to onPostexecute so no overhead on UI thread..setting text is not a difficult task in doInbackGround method.

OTHER TIPS

Problem is with this statement

icon.setImageDrawable(entry.loadIcon(mPackManager));

That is because this method loadIcon() is being called in UI thread. On UI thread if you are trying to decode the Bitmap it will cause hiccups while scrolling.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top