Code example for ViewGroup

Methods: addView, setVisibility

0
        } else { 
            this.imageLoader = sharedImageLoader;
        } 
 
        progressViewContainer = new FrameLayout(getContext());
        progressViewContainer.addView(buildProgressSpinnerView(getContext()));
 
        if (autoLoad) {
            loadImage(); 
        } else { 
            showProgressView(false); 
        } 
    } 
 
    protected View buildProgressSpinnerView(Context context) {
        ProgressBar loadingSpinner = new ProgressBar(context);
        loadingSpinner.setIndeterminate(true);
        if (this.progressDrawable == null) {
            this.progressDrawable = loadingSpinner.getIndeterminateDrawable();
        } else { 
            loadingSpinner.setIndeterminateDrawable(progressDrawable);
            if (progressDrawable instanceof AnimationDrawable) {
                ((AnimationDrawable) progressDrawable).start();
            } 
        } 
 
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                progressDrawable.getIntrinsicWidth(), progressDrawable.getIntrinsicHeight(),
                Gravity.CENTER);
        loadingSpinner.setLayoutParams(lp);
 
        return loadingSpinner;
    } 
 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // insert the progress view and its container as a sibling to this image view 
        if (progressViewContainer.getParent() == null) {
            // the container for the progress view must behave identically to this view during 
            // layouting, otherwise e.g. relative positioning via IDs will break 
            progressViewContainer.setLayoutParams(getLayoutParams());
 
            ViewGroup parent = (ViewGroup) getParent();
            int index = parent.indexOfChild(this);
            parent.addView(progressViewContainer, index + 1);
        } 
 
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    } 
 
    /** 
     * Use this method to trigger the image download if you had previously set autoLoad to false. 
     */ 
    public void loadImage() { 
        if (state != STATE_LOADING) {
            if (imageUrl == null) {
                throw new IllegalStateException(
                        "image URL is null; did you forget to set it for this view?"); 
            } 
            showProgressView(true); 
            imageLoader.loadImage(imageUrl, this, new DefaultImageLoaderHandler());
        } 
    } 
 
    public boolean isLoaded() { 
        return state == STATE_LOADED;
    } 
 
    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    } 
 
    /** 
     * Often you have resources which usually have an image, but some don't. For these cases, use 
     * this method to supply a placeholder drawable which will be loaded instead of a web image. 
     *  
     * @param imageResourceId 
     *            the resource of the placeholder image drawable 
     */ 
    public void setNoImageDrawable(int imageResourceId) {
        setImageDrawable(getContext().getResources().getDrawable(imageResourceId));
        showProgressView(false); 
    } 
 
    /** 
     * Reset to view to its initial state, i.e. hide the progress item and show the image. 
     */ 
    public void reset() { 
        showProgressView(false); 
    } 
 
    private void showProgressView(boolean show) {
        if (show) {
            state = STATE_LOADING;
            progressViewContainer.setVisibility(View.VISIBLE);
            setVisibility(View.INVISIBLE);
        } else { 
            state = STATE_DEFAULT;
            progressViewContainer.setVisibility(View.INVISIBLE);
            setVisibility(View.VISIBLE);
        } 
    } 
 
    private class DefaultImageLoaderHandler extends RemoteImageLoaderHandler {