Android Gallery View - 如何设置资源?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
向适配器添加一个新的构造函数,使其读取
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)
ornew 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.