【问题标题】:Android GridView - randomly position when scrollingAndroid GridView - 滚动时随机定位
【发布时间】:2012-09-21 09:42:52
【问题描述】:

我正在尝试使用 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;
}

【问题讨论】:

    标签: android gridview android-gridview


    【解决方案1】:
     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;
    
    
        }
    

    请详细了解网格视图和回收利用。网络中有很多教程,堆栈溢出中有很多教程

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 2020-09-25
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多