Android GridView-滚动时随机放置
作者:互联网
我正在尝试使用gridview使用this教程进行一些菜单设置.
不幸的是,当我滚动菜单时,菜单项的位置会随机变化.
我正在使用.xml显示图像和标题.
顺便说一句:有什么办法可以对GridView进行排序?
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
String tmp = menuItems[position];
gridView = new View(context);
gridView = inflater.inflate(R.layout.menuitem, null);
TextView label = (TextView) gridView.findViewById(R.id.menuitem_label);
label.setText(context.getString(context.getResources().getIdentifier("string/txt_"+tmp, null, context.getPackageName())));
ImageView img = (ImageView) gridView.findViewById(R.id.menuitem_image);
SVG svg_img = SVGParser.getSVGFromResource(context.getResources(), context.getResources().getIdentifier("raw/"+tmp, null, context.getPackageName()));
if (svg_img != null)
img.setImageDrawable(svg_img.createPictureDrawable());
} else {
gridView = (View) convertView;
}
return gridView;
}
解决方法:
public View getView(int position, View convertView, ViewGroup parent) {
View gridView;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = inflater.inflate(R.layout.menuitem, null);
}
else
{
gridView= convertView;
}
TextView label = (TextView) gridView.findViewById(R.id.menuitem_label);
label.setText(context.getString(context.getResources().getIdentifier("string/txt_"+tmp, null, context.getPackageName())));
ImageView img = (ImageView) gridView.findViewById(R.id.menuitem_image);
SVG svg_img = SVGParser.getSVGFromResource(context.getResources(), context.getResources().getIdentifier("raw/"+tmp, null, context.getPackageName()));
img.setImageDrawable(svg_img.createPictureDrawable());
return gridView;
}
请研究有关gridviews和回收的更多信息.网络中有大量的教程,以及堆栈溢出
我只是重新创建您的代码.我还没有尝试过
标签:android-gridview,gridview,android 来源: https://codeday.me/bug/20191031/1978978.html