Skip to content

Most visited

Recently visited

navigation
DisplayingBitmaps / src / com.example.android.displayingbitmaps / ui /

ImageDetailFragment.java

1
/*
2
 * Copyright (C) 2012 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.displayingbitmaps.ui;
18
 
19
import android.os.Bundle;
20
import android.support.v4.app.Fragment;
21
import android.view.LayoutInflater;
22
import android.view.View;
23
import android.view.View.OnClickListener;
24
import android.view.ViewGroup;
25
import android.widget.ImageView;
26
import android.widget.ProgressBar;
27
 
28
import com.example.android.displayingbitmaps.R;
29
import com.example.android.displayingbitmaps.util.ImageFetcher;
30
import com.example.android.displayingbitmaps.util.ImageWorker;
31
import com.example.android.displayingbitmaps.util.Utils;
32
 
33
/**
34
 * This fragment will populate the children of the ViewPager from {@link ImageDetailActivity}.
35
 */
36
public class ImageDetailFragment extends Fragment implements ImageWorker.OnImageLoadedListener {
37
    private static final String IMAGE_DATA_EXTRA = "extra_image_data";
38
    private String mImageUrl;
39
    private ImageView mImageView;
40
    private ProgressBar mProgressBar;
41
    private ImageFetcher mImageFetcher;
42
 
43
    /**
44
     * Factory method to generate a new instance of the fragment given an image number.
45
     *
46
     * @param imageUrl The image url to load
47
     * @return A new instance of ImageDetailFragment with imageNum extras
48
     */
49
    public static ImageDetailFragment newInstance(String imageUrl) {
50
        final ImageDetailFragment f = new ImageDetailFragment();
51
 
52
        final Bundle args = new Bundle();
53
        args.putString(IMAGE_DATA_EXTRA, imageUrl);
54
        f.setArguments(args);
55
 
56
        return f;
57
    }
58
 
59
    /**
60
     * Empty constructor as per the Fragment documentation
61
     */
62
    public ImageDetailFragment() {}
63
 
64
    /**
65
     * Populate image using a url from extras, use the convenience factory method
66
     * {@link ImageDetailFragment#newInstance(String)} to create this fragment.
67
     */
68
    @Override
69
    public void onCreate(Bundle savedInstanceState) {
70
        super.onCreate(savedInstanceState);
71
        mImageUrl = getArguments() != null ? getArguments().getString(IMAGE_DATA_EXTRA) : null;
72
    }
73
 
74
    @Override
75
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
76
            Bundle savedInstanceState) {
77
        // Inflate and locate the main ImageView
78
        final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
79
        mImageView = (ImageView) v.findViewById(R.id.imageView);
80
        mProgressBar = (ProgressBar) v.findViewById(R.id.progressbar);
81
        return v;
82
    }
83
 
84
    @Override
85
    public void onActivityCreated(Bundle savedInstanceState) {
86
        super.onActivityCreated(savedInstanceState);
87
 
88
        // Use the parent activity to load the image asynchronously into the ImageView (so a single
89
        // cache can be used over all pages in the ViewPager
90
        if (ImageDetailActivity.class.isInstance(getActivity())) {
91
            mImageFetcher = ((ImageDetailActivity) getActivity()).getImageFetcher();
92
            mImageFetcher.loadImage(mImageUrl, mImageView, this);
93
        }
94
 
95
        // Pass clicks on the ImageView to the parent activity to handle
96
        if (OnClickListener.class.isInstance(getActivity()) && Utils.hasHoneycomb()) {
97
            mImageView.setOnClickListener((OnClickListener) getActivity());
98
        }
99
    }
100
 
101
    @Override
102
    public void onDestroy() {
103
        super.onDestroy();
104
        if (mImageView != null) {
105
            // Cancel any pending image work
106
            ImageWorker.cancelWork(mImageView);
107
            mImageView.setImageDrawable(null);
108
        }
109
    }
110
 
111
    @Override
112
    public void onImageLoaded(boolean success) {
113
        // Set loading spinner to gone once image has loaded. Cloud also show
114
        // an error view here if needed.
115
        mProgressBar.setVisibility(View.GONE);
116
    }
117
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.