OSDN Git Service

Add new API demo for -swNNNdp resource qualifier.
[android-x86/development.git] / samples / ApiDemos / src / com / example / android / apis / support / app / FragmentMenuSupport.java
1 /*
2  * Copyright (C) 2011 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.apis.support.app;
18
19 import com.example.android.apis.R;
20
21 import android.support.v4.app.Fragment;
22 import android.support.v4.app.FragmentActivity;
23 import android.support.v4.app.FragmentManager;
24 import android.support.v4.app.FragmentTransaction;
25 import android.support.v4.view.MenuCompat;
26
27 import android.os.Bundle;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.widget.CheckBox;
34
35 /**
36  * Demonstrates how fragments can participate in the options menu.
37  */
38 public class FragmentMenuSupport extends FragmentActivity {
39     Fragment mFragment1;
40     Fragment mFragment2;
41     CheckBox mCheckBox1;
42     CheckBox mCheckBox2;
43
44     // Update fragment visibility when check boxes are changed.
45     final OnClickListener mClickListener = new OnClickListener() {
46         public void onClick(View v) {
47             updateFragmentVisibility();
48         }
49     };
50
51     @Override
52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         setContentView(R.layout.fragment_menu);
55
56         // Make sure the two menu fragments are created.
57         FragmentManager fm = getSupportFragmentManager();
58         FragmentTransaction ft = fm.beginTransaction();
59         mFragment1 = fm.findFragmentByTag("f1");
60         if (mFragment1 == null) {
61             mFragment1 = new MenuFragment();
62             ft.add(mFragment1, "f1");
63         }
64         mFragment2 = fm.findFragmentByTag("f2");
65         if (mFragment2 == null) {
66             mFragment2 = new Menu2Fragment();
67             ft.add(mFragment2, "f2");
68         }
69         ft.commit();
70
71         // Watch check box clicks.
72         mCheckBox1 = (CheckBox)findViewById(R.id.menu1);
73         mCheckBox1.setOnClickListener(mClickListener);
74         mCheckBox2 = (CheckBox)findViewById(R.id.menu2);
75         mCheckBox2.setOnClickListener(mClickListener);
76
77         // Make sure fragments start out with correct visibility.
78         updateFragmentVisibility();
79     }
80
81     @Override
82     protected void onRestoreInstanceState(Bundle savedInstanceState) {
83         super.onRestoreInstanceState(savedInstanceState);
84         // Make sure fragments are updated after check box view state is restored.
85         updateFragmentVisibility();
86     }
87
88     // Update fragment visibility based on current check box state.
89     void updateFragmentVisibility() {
90         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
91         if (mCheckBox1.isChecked()) ft.show(mFragment1);
92         else ft.hide(mFragment1);
93         if (mCheckBox2.isChecked()) ft.show(mFragment2);
94         else ft.hide(mFragment2);
95         ft.commit();
96     }
97
98     /**
99      * A fragment that displays a menu.  This fragment happens to not
100      * have a UI (it does not implement onCreateView), but it could also
101      * have one if it wanted.
102      */
103     public static class MenuFragment extends Fragment {
104
105         @Override
106         public void onCreate(Bundle savedInstanceState) {
107             super.onCreate(savedInstanceState);
108             setHasOptionsMenu(true);
109         }
110
111         @Override
112         public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
113             MenuItem item;
114             item = menu.add("Menu 1a");
115             MenuCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);
116             item = menu.add("Menu 1b");
117             MenuCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);
118         }
119     }
120
121     /**
122      * Second fragment with a menu.
123      */
124     public static class Menu2Fragment extends Fragment {
125
126         @Override
127         public void onCreate(Bundle savedInstanceState) {
128             super.onCreate(savedInstanceState);
129             setHasOptionsMenu(true);
130         }
131
132         @Override
133         public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
134             MenuItem item;
135             item = menu.add("Menu 2");
136             MenuCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);
137         }
138     }
139 }