我的GridView有问题。我用它作为我的应用程序的菜单。我创建了一个自定义适配器,将菜单项显示为带有下面标签的图片。问题是,没有任何理由,第一张图片和标签被分配到第10个项目,如图所示(图片只是占位符,直到我自己绘制):
我不知道是什么导致了这种情况,特别是当我点击菜单中的一些可用项目然后通过按后退按钮返回菜单时,问题神奇地消失了。
这是我的代码:
public class MainMenuFragment extends Fragment implements OnItemClickListener{
GridView grid;
FragmentManager manager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.main_menu_fragment, container, false);
manager = getActivity().getFragmentManager();
grid = (GridView) root.findViewById(R.id.gridView1);
grid.setAdapter(new MenuTileAdapter(getActivity()));
grid.setOnItemClickListener(this);
return root;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction trans = manager.beginTransaction();
switch (position){
case 0:
trans.replace(R.id.frag_container, new BasicSettingsFragment());
trans.addToBackStack(null);
trans.commit();
break;
case 1:
trans.replace(R.id.frag_container, new StretchingFragment());
trans.addToBackStack(null);
trans.commit();
break;
case 2:
trans.replace(R.id.frag_container, new SqueezingFragment());
trans.addToBackStack(null);
trans.commit();
break;
case 3:
trans.replace(R.id.frag_container, new BendingFragment());
trans.addToBackStack(null);
trans.commit();
break;
case 4:
trans.replace(R.id.frag_container, new CrackBrickingFragment());
trans.addToBackStack(null);
trans.commit();
break;
case 5:
trans.replace(R.id.frag_container, new TraverseDeformationFragment());
trans.addToBackStack(null);
trans.commit();
break;
case 6:
trans.replace(R.id.frag_container, new CuttingFragment());
trans.addToBackStack(null);
trans.commit();
break;
default:
break;
}
}
}
这是MenuTileAdapter
的getView方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null){
gridView = new View(context);
gridView = inflater.inflate(R.layout.menu_tile, null);
TextView label = (TextView) gridView.findViewById(R.id.textView1);
ImageView icon = (ImageView) gridView.findViewById(R.id.imageView1);
label.setText(this.labels[position]);
switch (position){
case 0:
icon.setImageResource(R.drawable.a1);
break;
case 1:
icon.setImageResource(R.drawable.a2);
break;
case 2:
icon.setImageResource(R.drawable.a3);
break;
case 3:
icon.setImageResource(R.drawable.a4);
break;
case 4:
icon.setImageResource(R.drawable.a5);
break;
case 5:
icon.setImageResource(R.drawable.a6);
break;
case 6:
icon.setImageResource(R.drawable.a7);
break;
case 7:
icon.setImageResource(R.drawable.a8);
break;
case 8:
icon.setImageResource(R.drawable.a9);
break;
case 9:
icon.setImageResource(R.drawable.a10);
break;
case 10:
icon.setImageResource(R.drawable.a11);
break;
case 11:
icon.setImageResource(R.drawable.a12);
break;
}
} else {
gridView = convertView;
}
return gridView;
}
我已经完成了这段代码,并且没有看到任何导致这个奇怪问题的内容。
答案 0 :(得分:1)
此部分就在这里
if (convertView == null){
gridView = new View(context);
gridView = inflater.inflate(R.layout.menu_tile, null);
TextView label = (TextView) gridView.findViewById(R.id.textView1);
ImageView icon = (ImageView) gridView.findViewById(R.id.imageView1);
label.setText(this.labels[position]);
switch (position){
case 0:
icon.setImageResource(R.drawable.a1);
break;
case 1:
icon.setImageResource(R.drawable.a2);
break;
case 2:
icon.setImageResource(R.drawable.a3);
break;
case 3:
icon.setImageResource(R.drawable.a4);
break;
case 4:
icon.setImageResource(R.drawable.a5);
break;
case 5:
icon.setImageResource(R.drawable.a6);
break;
case 6:
icon.setImageResource(R.drawable.a7);
break;
case 7:
icon.setImageResource(R.drawable.a8);
break;
case 8:
icon.setImageResource(R.drawable.a9);
break;
case 9:
icon.setImageResource(R.drawable.a10);
break;
case 10:
icon.setImageResource(R.drawable.a11);
break;
case 11:
icon.setImageResource(R.drawable.a12);
break;
}
} else {
gridView = convertView;
}
如果convertView
不为null,您只需将其提供给已回收的视图,而无需在其中放置图像和文本