我有一个显示设备上安装的所有应用程序的网格视图。
每当我尝试回收项目(使用 convertView == null 等)时,相同的图标和文本会在我的网格中多次显示。
这是我的代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
TextView txtName;
FrameLayout layout;
// if (convertView == null) {
imageView = new ImageView(myContext);
imageView.setLayoutParams(new GridView.LayoutParams(230, 230));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
txtName = new TextView(myContext);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
txtName.setLayoutParams(relativeParams);
txtName.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
// txtName.setPadding(8, 8, 8, 8);
layout = new FrameLayout(myContext);
// FrameLayout.LayoutParams layoutparams = new
// FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
// ViewGroup.LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL |
// Gravity.CENTER_VERTICAL);
// layout.setLayoutParams(layoutparams);
RelativeLayout layout2 = new RelativeLayout(myContext);
layout.addView(imageView);
layout.addView(layout2);
layout2.addView(txtName);
ResolveInfo resolveInfo = MyAppList.get(position);
imageView.setImageDrawable(resolveInfo.loadIcon(myPackageManager));
txtName.setText(resolveInfo.loadLabel(myPackageManager));
// } else {
// layout = (FrameLayout) convertView;
// }
return layout;
}
如果我不回收它们,它们会正确显示,但 gridview 很慢。
我的第二个问题是:如何将文本放在网格视图内的图标下?就像在原生 Android 启动器中一样?
这是我的 GridActivity:
public class AppListActivity extends Activity {
PackageManager myPackageManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myPackageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appIntentList = getPackageManager()
.queryIntentActivities(intent, 0);
GridView gridview = new GridView(this);
gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gridview.setNumColumns(4);
gridview.setHorizontalSpacing(30);
gridview.setVerticalSpacing(30);
gridview.setBackgroundColor(getResources().getColor(android.R.color.black));
gridview.setGravity(Gravity.CENTER);
gridview.setColumnWidth(60);
gridview.setPadding(10, 10, 10, 10);
gridview.setAdapter(new MyAdapter(this, appIntentList));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ResolveInfo cleckedResolveInfo = (ResolveInfo) parent
.getItemAtPosition(position);
ActivityInfo clickedActivityInfo = cleckedResolveInfo.activityInfo;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName(
clickedActivityInfo.applicationInfo.packageName,
clickedActivityInfo.name);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);
}
});
setContentView(gridview);
}
}