2012-09-21 84 views
3

我试图使用gridview来制作一些使用this教程的菜单。Android 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
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; 


    } 

请学习更多关于GridView的和回收。有一堆网络教程以及在这里堆栈溢出

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

+0

WORKS PERFECT !!!! :))) 非常感谢!!! – marverix