我有一个大问题让我发疯。我有一个安装了所有应用程序的 ListView,但滚动速度很慢,所以我想改进它。我试图放置一个线程,但它并没有解决问题。这是代码
应用适配器
public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
Holder holder;
public ApplicationAdapter(Context context, int textViewResourceId,
List<ApplicationInfo> appsList) {
super(context, textViewResourceId, appsList);
this.context = context;
this.appsList = appsList;
packageManager = context.getPackageManager();
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
final Holder holder;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.snippet_list_row, null);
holder = new Holder();
holder.appName = (TextView) view.findViewById(R.id.app_name);
holder.packageName = (TextView) view.findViewById(R.id.app_paackage);
holder.iconview = (ImageView) view.findViewById(R.id.app_icon);
view.setTag(holder);
}
else
{
holder = (Holder)view.getTag();
}
final ApplicationInfo data = appsList.get(position);
if (null != data) {
holder.appName.setText(data.loadLabel(packageManager));
holder.packageName.setText(data.packageName);
holder.iconview.setImageDrawable(data.loadIcon(packageManager));
}
return view;
}
static class Holder
{
TextView appName, packageName;
ImageView iconview;
}
}
活动
public class Activity_Eclair extends ListActivity {
public PackageManager packageManager = null;
public List<ApplicationInfo> applist = null;
public ApplicationAdapter listadaptor = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eclair);
ListView lv = getListView();
lv.setFastScrollEnabled(true);
lv.setScrollingCacheEnabled(false);
registerForContextMenu(lv);
packageManager = getPackageManager();
new LoadApplications().execute();
Button bottone1 = (Button)findViewById(R.id.button1);
bottone1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new LoadApplications().execute();
}
});};
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ApplicationInfo app = applist.get(position);
Uri packageUri = Uri.parse("package:"+app.packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageUri);
startActivity(uninstallIntent);
}
public List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
for (ApplicationInfo info : list) {
try {
if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
applist.add(info);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return applist;
}
private class LoadApplications extends AsyncTask<Void, Void, Void> {
public ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(Activity_Eclair.this,
R.layout.snippet_list_row, applist);
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
protected void onDestroy() {
if(progress!=null)
if(progress.isShowing()){
progress.dismiss();
}
}
@Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(Activity_Eclair.this, null,
"Loading...");
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
private final static int UPDATE_MENU_OPTION = 1;
private final static int DELETE_MENU_OPTION = 2;
private final static int TRUNCATE_MENU_OPTION = 3;
private final static int DELETE = 4;
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
final long examId = info.id;
ApplicationInfo app = applist.get((int) info.id);
switch (item.getItemId()) {
case UPDATE_MENU_OPTION:
try {
Intent intent = packageManager
.getLaunchIntentForPackage(app.packageName);
if (null != intent) {
startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(Activity_Eclair.this, e.getMessage(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(Activity_Eclair.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
return true;
case DELETE_MENU_OPTION:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id="+app.packageName));
startActivity(browserIntent);
return true;
case TRUNCATE_MENU_OPTION:
try {
//Open the specific App Info page:
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + app.packageName));
startActivity(intent);
} catch ( ActivityNotFoundException e ) {
//e.printStackTrace();
//Open the generic Apps page:
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
startActivity(intent);
}
return true;
case DELETE:
{
Uri packageUri = Uri.parse("package:"+app.packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageUri);
startActivity(uninstallIntent);
}
return true;
default:
return super.onContextItemSelected(item);
}
}
我声明我已经尝试过 StackOverflow 和 Web 上的许多片段,但都不起作用。}