关于Android画廊控制一般问题 [英] General Question about Android Gallery Control
问题描述
所以,我一直在做的XML布局,涉及得到一个屏幕上的图像水平滚动的行,并因此只使用horizontalscrollview一个项目。和一堆imagebuttons的。我用了一个包括换一种XML页面布局,然后另一个程序员将动态填充图像。
So, I've been doing xml layout for a project that involves getting a horizontal scrollable row of images on a screen, and did so using just a horizontalscrollview. and a bunch of imagebuttons. I used an include to put this on another xml layout page and another programmer will then populate the images dynamically.
我的问题是,如何将画廊控制我们受益?我没有做过太多的Java编程,我已经在网上看到了如何实现这种控制一些指示,但不是很多,为什么你会用这个。它看起来像这样通过控制阵列工作主要通过Java插入,但除此之外,我不能告诉有什么好处,是从阅读在我刚刚创造的布局和具有这种其他程序员手动插入自己的图像方式。
My question is, how would the gallery control benefit us? I haven't done much Java programming and I've seen some instruction online of how to implement this control, but not a lot on WHY you would use this. It looks like this control works mainly via Java insertion via array, but other than that I can't tell what the benefits are from reading over my way of just creating the layout and having this other programmer insert his own images manually.
另一个相关的问题 - 这些图片的画廊需要我imageviews,也可以是图像映射?目前他们是图像映射,因为我们希望他们clicable去一个用户的配置文件等。
Another related question - do these images for a gallery need to me imageviews, or can they be imagemaps? Currently they are imagemaps because we want them to be clicable to go to a user's profile, etc.
谢谢!
推荐答案
画廊几乎是完美的。在我的项目之一我有与它画廊的LinearLayout:
Gallery is nearly perfect. In one of my projects I do have a LinearLayout with a Gallery in it:
<Gallery
android:id="@+id/gallery"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_width="fill_parent"
android:spacing="2dip" />
这是活动实现OnItemClickListener:
An activity implements OnItemClickListener:
public class MyActivity extends Activity implements OnItemClickListener {
一个数据结构包含的所有项目,并发送到适配器:
A data structure contains all items and is send to an adapter:
private void processGallery() {
adapter = new MyAdapter(this, containers, appName);
if (adapter != null) {
gallery.setAdapter(adapter);
}
}
@Override
public void onItemClick(final AdapterView<?> adapterView, final View view, final int position, final long id) {
if (containers != null) {
container = containers.get(position);
if (container != null) {
// Handle selected image
}
}
}
该适配器通常BaseAdapter - 没有什么神奇的:
The adapter is a usual BaseAdapter - nothing magic:
public class MyAdapter extends BaseAdapter {
private ArrayList<Container> containers;
private Context context;
public int getCount() {
return containers.size();
}
public Object getItem(final int position) {
return containers.get(position);
}
public long getItemId(final int position) {
return position;
}
public View getView(final int position, final View contentView, final ViewGroup viewGroup) {
ImageView imageView = new ImageView(context);
Container container = containers.get(position);
if (container != null) {
// Do your image thing here
}
return imageView;
}
public MyAdapter(final Context context, final ArrayList<Container> containers, final String appName) {
this.context = context;
this.containers = containers;
}
}
这个简单的code给人以可点击项的水平滚动的图片库。点击被发送到活动 - 无需做一下适配器看中。我从这里出的DrawableCache,我使用的,因为我的项目来自网络的做code删除。
This simple code gives a horizontal scrolling image gallery with clickable items. The click is send to the activity - no need to do something fancy in the adapter. I removed from the code shown here a DrawableCache that I use because my items do come from the web.
这篇关于关于Android画廊控制一般问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!