Android,如何在Gallery的每个图像项目中添加标题(自定义视图)(来自图库适配器)?

时间:2012-04-27 02:47:37

标签: android view gallery adapter

我可以通过图库适配器在图库中显示我的图片。现在,我需要在其底部为每个图像添加标题。我不知道如何添加它。这是我的适配器代码:

    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;
    } 
} 

任何建议都将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

getView()中,您可以动态创建包含ImageView和TextView的布局,而不是动态创建ImageView,然后您可以适当填写:

    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");