【问题标题】:Gallery doesn't show anything when it has only 1 image画廊只有 1 张图片时不显示任何内容
【发布时间】:2013-07-19 23:25:05
【问题描述】:

我在第一个活动中使用SurfaceView。在使用SurfaceView 单击图像时,我会移动到第二个活动,同时存储最近单击图像的路径。在第二个活动中,我想在Gallery 中显示该图像。

在点击按钮的第二个活动中,我再次移动到第一个活动,点击图片后,我移动到第二个活动。

现在在第二个活动中,将有 2 张图片应该显示在 Gallery 中。

我的问题是点击第一张图片后,Gallery 没有显示任何内容。请注意,为Gallery 设置的适配器的getView() 正在被调用。

但是在点击第二张图片之后,当Gallery中有2张或更多张图片时,它们就会被显示出来。

我也试过g.refreshDrawableState();之后

g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(this);     
g.setSelection(0, false);

但无济于事。

我知道我在这里写的内容听起来很复杂,但请尝试将场景形象化。

任何帮助表示赞赏。

编辑

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mImageIds.length;//mImageIds is Array of image paths
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            final String imageUri = mImageIds[position].trim();
            try {
                if (imageUri.length() != 0) {
                    File f = new File(imageUri);
                    if (f.exists()) {
                        b = constants.shrinkBitmap(f.getPath(), 800, 800);
                        d = new BitmapDrawable(getResources(), b);
                        i.setImageDrawable(d);
                    } else {
                        i.setBackgroundColor(Color.WHITE);
                        i.setImageResource(R.drawable.ic_launcher);
                    }
                } else {
                    i.setBackgroundResource(R.drawable.ic_launcher);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            i.setAdjustViewBounds(true);
//BELOW "length" is some predefined value
            i.setPadding(Math.round(length * 0.05f), 0,
                    Math.round(length * 0.05f), 0);
            i.setLayoutParams(new Gallery.LayoutParams(Math
                    .round(length * 0.9f),
                    LayoutParams.WRAP_CONTENT));
            i.setScaleType(ScaleType.FIT_XY);
            return i;
        }
    }

编辑 (2)

Vikram 的建议下,我尝试记录 length 值,第一次确实是 0,即调用 i.setLayoutParams(new Gallery.LayoutParams(Math.round(length * 0.9f),LayoutParams.WRAP_CONTENT));

我通过这种方式在onCreate() 之后设置了这个length 变量:

g.post(new Runnable() {
     public void run() {
       length = g.getRight() - g.getLeft();
     }
    });

因为我想在代码中检索父布局的宽度并相应地设置图库项目宽度。

我知道我得到 length 为 0 的原因是 setContentView() 需要一些时间才能生效。如何在调用 Adapter 之前检索?

【问题讨论】:

  • 你能发布你的适配器代码吗?
  • 如果第一次点击图片,请使用 Logcat 检查mImageIds 的内容。在getView() 中执行此操作。我觉得你的适配器没问题。
  • mImageIds 确实有图像 URI。同样g.getChildCount 也给出 0+ 结果。
  • 仔细检查length的值。第一项可能设置为zero
  • 天哪。我怎么错过了。请检查我编辑的问题。

标签: android surfaceview android-gallery


【解决方案1】:

变量length 似乎是这里的问题。如果它的值为zero,当调用第一项的getView()时,width参数设置为zero

在您的编辑中,length 确实得到了一个非零值,可能有点晚了。您可以进行以下调用来更新视图:

myAdapter.notifyDataSetChanged();

在计算length 之后,这个调用应该放在run() 中。

“闪烁”的解决方法很好。作为替代方案(如果画廊的宽度应该与屏幕宽度匹配),试试这个:

代替:

g.post(new Runnable() {
 public void run() {
   length = g.getRight() - g.getLeft();
 }
});

放:

Point pointSize = new Point();
getWindowManager().getDefaultDisplay().getSize(pointSize);
length = pointSize.x;

在初始化适配器之前放置它。

这里要注意的另一件事是g.getRight() 计算为:

g.getRight() = g.getLeft() + g.getWidth()

所以,要计算length,你不妨这样做:

length = g.getWidth()

作为:

getWidth() = getRight() - getLeft()

【讨论】:

  • 对于Pointfunda,我得到The method getSize(Point) is undefined for type Display 错误。
  • 所以我使用了this que.接受的答案
猜你喜欢
  • 1970-01-01
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 2016-04-28
  • 1970-01-01
相关资源
最近更新 更多