| 1 |
/* |
| 2 |
* Copyright (C) 2014 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 |
* in compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 |
* or implied. See the License for the specific language governing permissions and limitations under |
| 12 |
* the License. |
| 13 |
*/ |
| 14 |
package ${packageName}; |
| 15 |
|
| 16 |
import android.app.Activity; |
| 17 |
import android.app.Fragment; |
| 18 |
import android.os.Bundle; |
| 19 |
import android.os.Handler; |
| 20 |
import android.view.Gravity; |
| 21 |
import android.view.LayoutInflater; |
| 22 |
import android.view.View; |
| 23 |
import android.view.ViewGroup; |
| 24 |
import android.widget.FrameLayout; |
| 25 |
import android.widget.ProgressBar; |
| 26 |
|
| 27 |
/* |
| 28 |
* BrowseErrorActivity shows how to use ErrorFragment |
| 29 |
*/ |
| 30 |
public class BrowseErrorActivity extends Activity { |
| 31 |
private static int TIMER_DELAY = 3000; |
| 32 |
private static int SPINNER_WIDTH = 100; |
| 33 |
private static int SPINNER_HEIGHT = 100; |
| 34 |
|
| 35 |
private ErrorFragment mErrorFragment; |
| 36 |
private SpinnerFragment mSpinnerFragment; |
| 37 |
|
| 38 |
/** |
| 39 |
* Called when the activity is first created. |
| 40 |
*/ |
| 41 |
@Override |
| 42 |
public void onCreate(Bundle savedInstanceState) { |
| 43 |
super.onCreate(savedInstanceState); |
| 44 |
setContentView(R.layout.${layoutName}); |
| 45 |
|
| 46 |
testError(); |
| 47 |
} |
| 48 |
|
| 49 |
private void testError() { |
| 50 |
mErrorFragment = new ErrorFragment(); |
| 51 |
getFragmentManager().beginTransaction().add(R.id.main_browse_fragment, mErrorFragment).commit(); |
| 52 |
|
| 53 |
mSpinnerFragment = new SpinnerFragment(); |
| 54 |
getFragmentManager().beginTransaction().add(R.id.main_browse_fragment, mSpinnerFragment).commit(); |
| 55 |
|
| 56 |
final Handler handler = new Handler(); |
| 57 |
handler.postDelayed(new Runnable() { |
| 58 |
@Override |
| 59 |
public void run() { |
| 60 |
getFragmentManager().beginTransaction().remove(mSpinnerFragment).commit(); |
| 61 |
mErrorFragment.setErrorContent(); |
| 62 |
} |
| 63 |
}, TIMER_DELAY); |
| 64 |
} |
| 65 |
|
| 66 |
static public class SpinnerFragment extends Fragment { |
| 67 |
@Override |
| 68 |
public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 69 |
Bundle savedInstanceState) { |
| 70 |
ProgressBar progressBar = new ProgressBar(container.getContext()); |
| 71 |
if (container instanceof FrameLayout) { |
| 72 |
FrameLayout.LayoutParams layoutParams = |
| 73 |
new FrameLayout.LayoutParams(SPINNER_WIDTH, SPINNER_HEIGHT, Gravity.CENTER); |
| 74 |
progressBar.setLayoutParams(layoutParams); |
| 75 |
} |
| 76 |
return progressBar; |
| 77 |
} |
| 78 |
} |
| 79 |
} |