• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 import com.example.android.displayingbitmaps.R;
28 import com.example.android.displayingbitmaps.util.ImageFetcher;
29 import com.example.android.displayingbitmaps.util.ImageWorker;
30 import com.example.android.displayingbitmaps.util.Utils;
31 
32 /**
33  * This fragment will populate the children of the ViewPager from {@link ImageDetailActivity}.
34  */
35 public class ImageDetailFragment extends Fragment {
36     private static final String IMAGE_DATA_EXTRA = "extra_image_data";
37     private String mImageUrl;
38     private ImageView mImageView;
39     private ImageFetcher mImageFetcher;
40 
41     /**
42      * Factory method to generate a new instance of the fragment given an image number.
43      *
44      * @param imageUrl The image url to load
45      * @return A new instance of ImageDetailFragment with imageNum extras
46      */
newInstance(String imageUrl)47     public static ImageDetailFragment newInstance(String imageUrl) {
48         final ImageDetailFragment f = new ImageDetailFragment();
49 
50         final Bundle args = new Bundle();
51         args.putString(IMAGE_DATA_EXTRA, imageUrl);
52         f.setArguments(args);
53 
54         return f;
55     }
56 
57     /**
58      * Empty constructor as per the Fragment documentation
59      */
ImageDetailFragment()60     public ImageDetailFragment() {}
61 
62     /**
63      * Populate image using a url from extras, use the convenience factory method
64      * {@link ImageDetailFragment#newInstance(String)} to create this fragment.
65      */
66     @Override
onCreate(Bundle savedInstanceState)67     public void onCreate(Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69         mImageUrl = getArguments() != null ? getArguments().getString(IMAGE_DATA_EXTRA) : null;
70     }
71 
72     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)73     public View onCreateView(LayoutInflater inflater, ViewGroup container,
74             Bundle savedInstanceState) {
75         // Inflate and locate the main ImageView
76         final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
77         mImageView = (ImageView) v.findViewById(R.id.imageView);
78         return v;
79     }
80 
81     @Override
onActivityCreated(Bundle savedInstanceState)82     public void onActivityCreated(Bundle savedInstanceState) {
83         super.onActivityCreated(savedInstanceState);
84 
85         // Use the parent activity to load the image asynchronously into the ImageView (so a single
86         // cache can be used over all pages in the ViewPager
87         if (ImageDetailActivity.class.isInstance(getActivity())) {
88             mImageFetcher = ((ImageDetailActivity) getActivity()).getImageFetcher();
89             mImageFetcher.loadImage(mImageUrl, mImageView);
90         }
91 
92         // Pass clicks on the ImageView to the parent activity to handle
93         if (OnClickListener.class.isInstance(getActivity()) && Utils.hasHoneycomb()) {
94             mImageView.setOnClickListener((OnClickListener) getActivity());
95         }
96     }
97 
98     @Override
onDestroy()99     public void onDestroy() {
100         super.onDestroy();
101         if (mImageView != null) {
102             // Cancel any pending image work
103             ImageWorker.cancelWork(mImageView);
104             mImageView.setImageDrawable(null);
105         }
106     }
107 }
108