【问题标题】:android gridview scrolling image misplaceandroid gridview滚动图像错位
【发布时间】:2014-07-31 00:45:48
【问题描述】:

我在使用 gridview 时遇到问题。我正在使用一个非常简单的 GridView,其中有图像和下面的 textView。 我的问题是 - 我每行显示 3 张图像,屏幕上一次显示 12 张图像,但是对于第 13 张图像,图像是正确的,但下面的文本是第 1 张图像。 如果我每行使用 4 个图像(我不想这样做),那么显示的文本是正确的。 同样在滚动gridview交换的图像和文本之后

这是我的自定义 gridview.java 代码

public class CustomGrid extends BaseAdapter{
  private Context mContext;
  private final String[] web;
  private final int[] Imageid; 

    public CustomGrid(Context c,String[] web,int[] Imageid ) {
        mContext = c;
        this.Imageid = Imageid;
        this.web = web;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return web.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {  

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid_single, null);
            TextView textView = (TextView) grid.findViewById(R.id.grid_text);
            ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
            textView.setText(web[position]);
            imageView.setImageResource(Imageid[position]);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

请尽快回复

【问题讨论】:

    标签: android android-layout gridview


    【解决方案1】:

    可能您需要更改代码如下。对于第 13 项转换视图不为空,因此它返回旧的第 1 视图。

    @覆盖 public View getView(int position, View convertView, ViewGroup parent) { // TODO 自动生成的方法存根 查看网格; LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {  
    
            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid_single, null);
            TextView textView = (TextView) grid.findViewById(R.id.grid_text);
            ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
    
        } else {
            grid = (View) convertView;
        }
            textView.setText(web[position]);
            imageView.setImageResource(Imageid[position]);
        return grid;
    

    【讨论】:

      【解决方案2】:

      效果很好

      @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              // TODO Auto-generated method stub
               View grid; 
               LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  if (convertView == null) {  
      
                      grid = new View(mContext);
                      grid = inflater.inflate(R.layout.grid_single, null);
      
                  } else {
                      grid = (View) convertView;
                  }
                  TextView textView = (TextView) grid.findViewById(R.id.grid_text);
                   ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
                      textView.setText(web[position]);
                      imageView.setImageResource(Imageid[position]);
                  return grid;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多