OSDN Git Service

Extract support lib samples out of ApiDemos and in to their own app.
[android-x86/development.git] / samples / Support4Demos / src / com / example / android / supportv4 / app / FragmentHideShowSupport.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.supportv4.app;
18
19 import com.example.android.supportv4.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
26 import android.os.Bundle;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.View.OnClickListener;
31 import android.widget.Button;
32 import android.widget.TextView;
33
34 /**
35  * Demonstration of hiding and showing fragments.
36  */
37 public class FragmentHideShowSupport extends FragmentActivity {
38
39     @Override
40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         setContentView(R.layout.fragment_hide_show_support);
43
44         // The content view embeds two fragments; now retrieve them and attach
45         // their "hide" button.
46         FragmentManager fm = getSupportFragmentManager();
47         addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
48         addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));
49     }
50
51     void addShowHideListener(int buttonId, final Fragment fragment) {
52         final Button button = (Button)findViewById(buttonId);
53         button.setOnClickListener(new OnClickListener() {
54             public void onClick(View v) {
55                 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
56                 ft.setCustomAnimations(android.R.anim.fade_in,
57                         android.R.anim.fade_out);
58                 if (fragment.isHidden()) {
59                     ft.show(fragment);
60                     button.setText("Hide");
61                 } else {
62                     ft.hide(fragment);
63                     button.setText("Show");
64                 }
65                 ft.commit();
66             }
67         });
68     }
69
70     public static class FirstFragment extends Fragment {
71         TextView mTextView;
72
73         @Override
74         public View onCreateView(LayoutInflater inflater, ViewGroup container,
75                 Bundle savedInstanceState) {
76             View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
77             View tv = v.findViewById(R.id.msg);
78             ((TextView)tv).setText("The fragment saves and restores this text.");
79
80             // Retrieve the text editor, and restore the last saved state if needed.
81             mTextView = (TextView)v.findViewById(R.id.saved);
82             if (savedInstanceState != null) {
83                 mTextView.setText(savedInstanceState.getCharSequence("text"));
84             }
85             return v;
86         }
87
88         @Override
89         public void onSaveInstanceState(Bundle outState) {
90             super.onSaveInstanceState(outState);
91
92             // Remember the current text, to restore if we later restart.
93             outState.putCharSequence("text", mTextView.getText());
94         }
95     }
96
97     public static class SecondFragment extends Fragment {
98
99         @Override
100         public View onCreateView(LayoutInflater inflater, ViewGroup container,
101                 Bundle savedInstanceState) {
102             View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
103             View tv = v.findViewById(R.id.msg);
104             ((TextView)tv).setText("The TextView saves and restores this text.");
105
106             // Retrieve the text editor and tell it to save and restore its state.
107             // Note that you will often set this in the layout XML, but since
108             // we are sharing our layout with the other fragment we will customize
109             // it here.
110             ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true);
111             return v;
112         }
113     }
114 }