我根据一些例子拼凑起来,结果是一团糟。图像/文本相互重叠,像单个列表视图项一样来回移动:没有像图像列表那样。我的目标是一张下面有文本视图的图像。就这些。请注意:我还尝试从适配器中的 getView() 返回一个膨胀的视图,结果几乎相同。我开始怀疑 Gallery 是否真的可以支持不仅仅是 ImageView 作为类型。
package com.example.elgallery;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private GalleryAdapter mGalleryAdapter;
private ArrayList<GalleryItem> mGalleryItems;
private Gallery mGallery;
public class GalleryItem{
int imageId=R.drawable.ic_launcher;
String caption;
public int getImageId() {
return imageId; }
public String getCaption() {
return caption;
}
public GalleryItem(int i,String s) {
imageId=i;
caption=s;
}
}
int[] resourceImages = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GalleryItem[] item = new GalleryItem[6];
mGalleryItems = new ArrayList<GalleryItem>();
//initialising all items, change member variables according to needs
for(int i=0;i<6;i++){
mGalleryItems.add(new GalleryItem(resourceImages[i], "pic no" +(i+1)));
}
mGallery = (Gallery)findViewById(R.id.gallery);
mGalleryAdapter = new GalleryAdapter(this);
mGalleryAdapter.setGalleryItems(mGalleryItems);
mGallery.setAdapter(mGalleryAdapter);
mGalleryAdapter.notifyDataSetChanged();
}
private class GalleryAdapter extends BaseAdapter {
private Context mContext;
mArrayList<GalleryItem> galleryItems;
public ArrayList<GalleryItem> getGalleryItems() {
return galleryItems;
}
public void setGalleryItems(ArrayList<GalleryItem> galleryItems) {
this.galleryItems = galleryItems;
}
public GalleryAdapter (Context context)
{
mContext = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return galleryItems.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return galleryItems.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ImageView i = new ImageView(ll.getContext());
i.setTag("someTage"); //i.setImageURI(mUrls[position]);
i.setImageResource(R.drawable.ic_launcher);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(48, 48));
TextView tv = new TextView(ll.getContext());
tv.setTag("someTag2"); tv.setText("someText");
tv.setLayoutParams(new Gallery.LayoutParams(48, 48));
ll.addView(tv);
return ll;
}
}
}
这是 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="200dip"
/>
</LinearLayout>
结果:一团糟。看起来像三个图像和文本视图在彼此之上。