Android GridView - 滚动时随机定位

时间:2012-09-21 09:42:52

标签: android gridview android-gridview

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

不幸的是,当我滚动菜单时,菜单项的位置随机变化。 我正在使用.xml来显示图片和标题。

BTW:有没有办法对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;
}

1 个答案:

答案 0 :(得分: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;


    }

请详细了解gridviews和回收。网络中有很多教程以及堆栈溢出

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