Home | History | Annotate | Download | only in app
      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.supportv4.app;
     18 
     19 import com.example.android.supportv4.R;
     20 
     21 import android.support.v4.app.Fragment;
     22 import android.support.v4.app.FragmentManager;
     23 import android.support.v4.app.FragmentTransaction;
     24 
     25 import android.os.Bundle;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.view.View.OnClickListener;
     30 import android.widget.CheckBox;
     31 
     32 /**
     33  * Demonstrates how fragments can participate in the options menu.
     34  */
     35 public class FragmentMenuFragmentSupport extends Fragment {
     36     Fragment mFragment1;
     37     Fragment mFragment2;
     38     CheckBox mCheckBox1;
     39     CheckBox mCheckBox2;
     40 
     41     // Update fragment visibility when check boxes are changed.
     42     final OnClickListener mClickListener = new OnClickListener() {
     43         public void onClick(View v) {
     44             updateFragmentVisibility();
     45         }
     46     };
     47 
     48     @Override
     49     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     50             Bundle savedInstanceState) {
     51         View v = inflater.inflate(R.layout.fragment_menu, container, false);
     52 
     53         // Make sure the two menu fragments are created.
     54         FragmentManager fm = getChildFragmentManager();
     55         FragmentTransaction ft = fm.beginTransaction();
     56         mFragment1 = fm.findFragmentByTag("f1");
     57         if (mFragment1 == null) {
     58             mFragment1 = new FragmentMenuSupport.MenuFragment();
     59             ft.add(mFragment1, "f1");
     60         }
     61         mFragment2 = fm.findFragmentByTag("f2");
     62         if (mFragment2 == null) {
     63             mFragment2 = new FragmentMenuSupport.Menu2Fragment();
     64             ft.add(mFragment2, "f2");
     65         }
     66         ft.commit();
     67 
     68         // Watch check box clicks.
     69         mCheckBox1 = (CheckBox)v.findViewById(R.id.menu1);
     70         mCheckBox1.setOnClickListener(mClickListener);
     71         mCheckBox2 = (CheckBox)v.findViewById(R.id.menu2);
     72         mCheckBox2.setOnClickListener(mClickListener);
     73 
     74         // Make sure fragments start out with correct visibility.
     75         updateFragmentVisibility();
     76 
     77         return v;
     78     }
     79 
     80     @Override
     81     public void onViewStateRestored(Bundle savedInstanceState) {
     82         super.onViewStateRestored(savedInstanceState);
     83         // Make sure fragments are updated after check box view state is restored.
     84         updateFragmentVisibility();
     85     }
     86 
     87     // Update fragment visibility based on current check box state.
     88     void updateFragmentVisibility() {
     89         FragmentTransaction ft = getChildFragmentManager().beginTransaction();
     90         if (mCheckBox1.isChecked()) ft.show(mFragment1);
     91         else ft.hide(mFragment1);
     92         if (mCheckBox2.isChecked()) ft.show(mFragment2);
     93         else ft.hide(mFragment2);
     94         ft.commit();
     95     }
     96 }
     97