Android,如何将标题(自定义视图)添加到Gallery的每个图像项(从图库适配器)?

问题描述:

我可以通过图库适配器在画廊内显示我的图像。现在,我需要将标题添加到底部的每个图像。我不知道如何添加它。这是我的适配器代码:Android,如何将标题(自定义视图)添加到Gallery的每个图像项(从图库适配器)?

public class LensaPhotoAdapter_KM extends BaseAdapter{ 

    private int mGalleryItemBackground; 

    private Context context; 

    private ImageLoader imageLoader; 
// private File cacheDir; 
    private ImageLoaderConfiguration config; 
    private DisplayImageOptions options; 
    private ArrayList<String> imageURLs = new ArrayList<String>(); 
    private ArrayList<String> imageTitle = new ArrayList<String>(); 


    public LensaPhotoAdapter_KM(Context context){ 
     Log.i("LensaPhotoAdapter", "Try to build the screen..."); 

     this.context = context; 

     TypedArray attr = context.obtainStyledAttributes(R.styleable.HelloGallery); 
     mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0); 
     attr.recycle(); 

     // Get singleton instance of ImageLoader 
     imageLoader = ImageLoader.getInstance(); 
    } 



    public void setData(ImageFeedItemList imageFeedItemList){ 
     int numberOfItems = imageFeedItemList.getHeaderImage().size(); 

     imageTitle = imageFeedItemList.getTitle(); 

//  for(String str: imageFeedItemList.getHeaderImage()) 
//   Log.i("title is:", str); 

     for(int i=0; i<numberOfItems; i++){ 
      String str = imageFeedItemList.getTitle().get(i); 
      str = str.substring(0, str.indexOf(" ")); 
      if(str.equalsIgnoreCase("Konsert")) 
       imageURLs.add(imageFeedItemList.getHeaderImage().get(i)); 

//   for(String str1: imageURLs) 
//    Log.i("title is:", str1); 
     } 

//  cacheDir = new File(Environment.getExternalStorageDirectory(), "UniversalImageLoader/Cache"); 

     // Create configuration for ImageLoader 
     config = new ImageLoaderConfiguration.Builder(context) 
        .maxImageWidthForMemoryCache(800) 
        .maxImageHeightForMemoryCache(800) 
        .httpConnectTimeout(5000) 
        .httpReadTimeout(30000) 
        .threadPoolSize(5) 
        .threadPriority(Thread.MIN_PRIORITY + 2) 
        .denyCacheImageMultipleSizesInMemory() 
        .memoryCache(new UsingFreqLimitedMemoryCache(2000000)) // You can pass your own memory cache implementation 
//     .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation 
        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) 
        .build(); 

     // Creates display image options for custom display task 
     options = new DisplayImageOptions.Builder() 
        .showStubImage(R.drawable.icon_loading) 
        .showImageForEmptyUrl(R.drawable.icon_remove) 
        .cacheInMemory() 
        .cacheOnDisc() 
        .decodingType(DecodingType.MEMORY_SAVING) 
        .build(); 

     // Initialize ImageLoader with created configuration. Do it once. 
     imageLoader.init(config); 
    } 



    //---returns the number of images--- 
    public int getCount() { 
     return imageURLs.size(); 
    } 

    //---returns the ID of an item--- 
    public Object getItem(int position) { 
     return null; 
    } 

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

    //---returns an ImageView view--- 
    public View getView(int position, View convertView, ViewGroup parent){  
     ImageView imageView = new ImageView(context); 
     imageView.setLayoutParams(new Gallery.LayoutParams(250, 250)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
     imageView.setBackgroundResource(mGalleryItemBackground); 

     // Load and display image 
     String imageUrl = imageURLs.get(position); 
     imageLoader.displayImage(imageUrl, imageView, options); 

     return imageView; 
    } 
} 

任何建议,将不胜感激。由于

在你getView(),而不是在飞行创建ImageView,你可以膨胀同时具有一个的ImageView和一个TextView,然后你就可以适当地填写布局:

View container = inflater.inflate(R.layout.the_layout, parent, false); 
    ImageView image = ll.findViewById(R.id.image); 
    String imageUrl = imageURLs.get(position); 
    imageLoader.displayImage(imageUrl, imageView, options); 
    TextView text = ll.findViewById(R.id.text); 
    text.setText("whatever"); 
+0

谢谢妖,其实我之前就是这么做的,结果很好。但我喜欢看图像周围的框架。 – Hesam 2012-04-27 03:49:28

+0

哦,我通过添加convertView.setLayoutParams(new Gallery.LayoutParams(250,250))来实现它; \t \t \t \t convertView.setBackgroundResource(mGalleryItemBackground); – Hesam 2012-04-27 03:50:06

+0

感谢它的工作正常:) – Hesam 2012-04-27 03:50:19