3

我正在尝试使用 gridview 使用教程制作一些菜单。

不幸的是,当我滚动菜单时,菜单项的位置是随机的。我正在使用 .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;
}
4

1 回答 1

4
 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;


    }

请研究更多关于网格视图和回收的信息。网络中有很多教程以及堆栈溢出中的教程

我只是重新创建你的代码。我还没试过

于 2012-09-21T10:05:19.457 回答