I tried creating a simple launcher which displays the list of apps in a GridView
and the GridView doesn't have a smooth scrolling. The call to resolveInfo.activityInfo.loadIcon(pm)
seems to be the one causing the lag.
Activity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent packIntent = new Intent(Intent.ACTION_MAIN,null);
packIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final PackageManager pm = getPackageManager();
List<ResolveInfo> packList= pm.queryIntentActivities(packIntent, 0 );
GridView appGrid = (GridView) findViewById(R.id.appGrid);
GridAdapter gridAdapter = new GridAdapter(this, pm, packList);
appGrid.setAdapter(gridAdapter);
}
}
GridAdapter
public class GridAdapter extends BaseAdapter{
private PackageManager pm;
private List<ResolveInfo> packList;
private Context mContext;
public GridAdapter(Context mContext,PackageManager pm, List<ResolveInfo> packList) {
super();
this.mContext=mContext;
this.pm = pm;
this.packList = packList;
}
@Override
public int getCount() {
return packList.size();
}
@Override
public Object getItem(int position) {
return packList.get(position);
}
@Override
public long getItemId(int arg0) {
return 0;
}
static class ViewHolder{
TextView text;
ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView==null){
convertView = li.inflate(R.layout.drawer_item, null);
viewHolder = new ViewHolder();
viewHolder.text= (TextView)convertView.findViewById(R.id.icon_text);
viewHolder.image= (ImageView)convertView.findViewById(R.id.icon_image);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(packList.get(position).loadLabel(pm));
viewHolder.image.setImageDrawable(packList.get(position).activityInfo.loadIcon(pm));
return convertView;
}
}
activity_main.xml
<GridView
android:id="@+id/appGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:columnWidth="80dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</RelativeLayout>
and drawer_item.xml
<ImageView
android:id="@+id/icon_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="3dp"/>
<TextView
android:id="@+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:maxLines="2"
android:gravity="center_horizontal"
/>
</LinearLayout>
I tried creating a custom bean which has
class AppDetails{
Drawable icon;
String label;
}
and tried populating a list of these
for(int i = 0; i<packList.size();i++){
AppDetails temp = new AppDetails();
temp.icon=packList.get(i).activityInfo.loadIcon(pm);
temp.label=packList.get(i).loadLabel(pm).toString();
appDetails.add(temp);
}
and tried building an adapter for the same. If I do this, the GridView scroll becomes smoother, but it slows down the Activity
. Tried using AyncTasks to load the icon, but icons take time to get loaded via the AsyncTask..
Is there a better way of loading the App icons or caching it?