2012-09-21 133 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