1

我正在启动一项活动,以启动所有已安装应用程序的列表以及应用程序图标和名称,它运行良好,只是在滚动应用程序时非常滞后。有什么办法让它顺利吗?这是我的代码,谢谢。

public class AppListsActivity extends ListActivity{
    static final String TAG = "AppListsActivity";
    private Context self = this;
    public static boolean progressBarStatus = false;

    SharedPreferences.Editor editor;
    PackageManager pm;
    AppAdapter adapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.apps_list);
        ListView lV = (ListView)findViewById(android.R.id.list);
        lV.setScrollingCacheEnabled(false);

        pm = getPackageManager();
        Intent main=new Intent(Intent.ACTION_MAIN, null); 
        main.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> launchables = pm.queryIntentActivities(main, 0);
        Collections.sort(launchables,new ResolveInfo.DisplayNameComparator(pm)); 

        adapter = new AppAdapter(pm, launchables);
        setListAdapter(adapter);

        progressBarStatus = true;
    }
    @Override
    protected void onListItemClick(ListView listView, View v, int position, long id) {
        //super.onListItemClick(l, v, position, id);

        ResolveInfo launchable = adapter.getItem(position);
        ActivityInfo activity = launchable.activityInfo;
        String selectionPname = activity.applicationInfo.packageName;
        String selection = activity.applicationInfo.loadLabel(pm).toString();
        Drawable selectionIcon = activity.applicationInfo.loadIcon(pm);

        editor = self.getSharedPreferences(WidgetConfigureActivity.PREFS_NAME, 0).edit();
        //switch (SettingsActivity.btnShortcutNo) {
        switch (WidgetConfigureActivity.btnShortcutNo) {
        case 1:
            //SettingsActivity.textTime.setText(selection);
            WidgetConfigureActivity.textTime.setText(selection);
            WidgetConfigureActivity.iconShortcutTime.setImageDrawable(selectionIcon);
            editor.putString("shortcutTimeWidget", selectionPname);         
            //editor.putString("shortcutTime", selection);
            editor.commit();
            MyWidgetProvider.mFisrtReboot = true;
            finish();
            break;
        default: 
            break;
        }       
    }
    private static class ViewHolder {
        public TextView label;
        public ImageView icon;
    }
    private class AppAdapter extends ArrayAdapter<ResolveInfo> {
        private PackageManager pm = null;
        Bitmap bmIcon;

        AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
            super(AppListsActivity.this, R.layout.list_row, apps);
            this.pm = pm;
        }       
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v==null) {
                v = newView(parent);
                ViewHolder viewHolder = new ViewHolder();
                viewHolder.label = (TextView) v.findViewById(R.id.label);
                viewHolder.icon = (ImageView) v.findViewById(R.id.icon);
                v.setTag(viewHolder);
            }
            ViewHolder holder = (ViewHolder) v.getTag();
            bmIcon = ((BitmapDrawable)getItem(position).loadIcon(pm)).getBitmap();
            bmIcon = Bitmap.createScaledBitmap(bmIcon, 5, 5, true);
            bindView(position, holder);       
            return(v);
        }       
        private View newView(ViewGroup parent) {
            return(getLayoutInflater().inflate(R.layout.list_row, parent, false));
        }       
        private void bindView(int position, ViewHolder holder) {                                 
            holder.label.setText(getItem(position).loadLabel(pm));              
            //holder.icon.setImageDrawable(getItem(position).loadIcon(pm));
            holder.icon.setImageBitmap(bmIcon);
        }
    }
4

0 回答 0