Android Gallery View - 如何设置资源?

发布于 2024-10-03 19:18:58 字数 1349 浏览 3 评论 0原文

我是 Java 编程/Android 开发的新手 - 但我一直在研究一些示例,并一直在尝试构建一个具有 3 个滚动厨房视图的“混合搭配”应用程序。

我创建了具有 3 个图库视图的相对布局,并定义了 3 个数组来包含图像列表。

我的问题是,我似乎无法让 ImageAdapter 为每个图库视图设置正确的图像资源(它在所有 3 个图库中复制相同的图像)。

如果我可以发布相关的代码部分:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.main);

    Gallery gTop = (Gallery) findViewById(R.id.gallery_top);
    Gallery gMid = (Gallery) findViewById(R.id.gallery_mid);
    Gallery gEnd = (Gallery) findViewById(R.id.gallery_end);

    gTop.setAdapter(new ImageAdapter(this));
    gMid.setAdapter(new ImageAdapter(this));
    gEnd.setAdapter(new ImageAdapter(this));
}

然后另一个类设置资源(硬编码):

 public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        i.setImageResource(mEndThumbIds[position]);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        );
        return i;
    }

如何删除此硬编码链接并检查哪个对象正在调用 getView 并相应地设置图像资源?

谢谢,亚历克斯

I am new to Java programming / Android development - but I have been working through a few examples and have been trying to build a "mix-and-match" app with 3 scrolling galley views.

I have created the relative layout with 3 gallery views, and have defined 3 arrays to contain a list of images.

My problem is that I can't seem to get the ImageAdapter to set the correct image resource for each gallery view (it duplicates the same images across all 3 galleries).

If I can just post the relevant sections of code:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.main);

    Gallery gTop = (Gallery) findViewById(R.id.gallery_top);
    Gallery gMid = (Gallery) findViewById(R.id.gallery_mid);
    Gallery gEnd = (Gallery) findViewById(R.id.gallery_end);

    gTop.setAdapter(new ImageAdapter(this));
    gMid.setAdapter(new ImageAdapter(this));
    gEnd.setAdapter(new ImageAdapter(this));
}

Then another class sets the resources (hardcoded):

 public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        i.setImageResource(mEndThumbIds[position]);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        );
        return i;
    }

How can I remove this hardcoded link and check to see what object is calling getView and set the image resources accordingly?

Thanks, Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2024-10-10 19:18:58

向适配器添加一个新的构造函数,使其读取 new ImageAdapter(this, Type.END)new ImageAdapter(this, Type.BEGIN) 怎么样?

当然,您可以定义一个enum来进行切换。

然后,在构造函数中只需检查请求的是哪一个并设置列表。

How about you add a new constructor to your adapter so that it reads new ImageAdapter(this, Type.END) or new ImageAdapter(this, Type.BEGIN)?

Of course you would define a enum to make the switch.

Then, in the constructor simply check which one is requested and set the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文