我有一个gridview,显示设备上安装的所有应用。
每当我尝试回收项目时(使用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非常慢。
我的第二个问题是:如何将文本放在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);
}
}
答案 0 :(得分:1)
嗯,您需要在列表项设置之外移动列表项的数据填充。类似的东西:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
TextView txtName;
RelativeLayout 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);
generateAndSetViewId(imageView);
txtName = new TextView(myContext);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeParams.addRule(RelativeLayout.BELOW, imageView.getId());
txtName.setLayoutParams(relativeParams);
txtName.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
// txtName.setPadding(8, 8, 8, 8);
layout = new RelativeLayout(myContext);
// FrameLayout.LayoutParams layoutparams = new
// FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
// ViewGroup.LayoutParams.FILL_PARENT, Gravity.CENTER_HORIZONTAL |
// Gravity.CENTER_VERTICAL);
// layout.setLayoutParams(layoutparams);
layout.addView(imageView);
layout.addView(txtName);
} else {
layout = (RelativeLayout) convertView;
imageView = (ImageView) layout.getChildAt(0);
txtName = (TextView) layout.getChildAt(1);
}
ResolveInfo resolveInfo = MyAppList.get(position);
imageView.setImageDrawable(resolveInfo.loadIcon(myPackageManager));
txtName.setText(resolveInfo.loadLabel(myPackageManager));
return layout;
}
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
@SuppressLint("NewApi")
public static int generateAndSetViewId(View view) {
int viewID = -1;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
viewID = View.generateViewId();
} else {
while (true) {
final int result = sNextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF) {
newValue = 1; // Roll over to 1, not 0.
}
if (sNextGeneratedId.compareAndSet(result, newValue)) {
viewID = result;
break;
}
}
}
view.setId(viewID);
return viewID;
}
如果您从XML中扩展视图而不是动态创建视图,那么上面的代码可以简化很多。