The file containing the source code shown below is located in the corresponding directory in <sdk>/samples/android-<version>/...
/* * Copyright (C) 2011 The Android开源工程 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.supportv4.app; import com.example.android.supportv4.R; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; /** * Demonstration of hiding and showing fragments. */ public class FragmentHideShowSupport extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_hide_show_support); // The content view embeds two fragments; now retrieve them and attach // their "hide" button. FragmentManager fm = getSupportFragmentManager(); addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1)); addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2)); } void addShowHideListener(int buttonId, final Fragment fragment) { final Button button = (Button)findViewById(buttonId); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out); if (fragment.isHidden()) { ft.show(fragment); button.setText("Hide"); } else { ft.hide(fragment); button.setText("Show"); } ft.commit(); } }); } public static class FirstFragment extends Fragment { TextView mTextView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.labeled_text_edit, container, false); View tv = v.findViewById(R.id.msg); ((TextView)tv).setText("The fragment saves and restores this text."); // Retrieve the text editor, and restore the last saved state if needed. mTextView = (TextView)v.findViewById(R.id.saved); if (savedInstanceState != null) { mTextView.setText(savedInstanceState.getCharSequence("text")); } return v; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Remember the current text, to restore if we later restart. outState.putCharSequence("text", mTextView.getText()); } } public static class SecondFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.labeled_text_edit, container, false); View tv = v.findViewById(R.id.msg); ((TextView)tv).setText("The TextView saves and restores this text."); // Retrieve the text editor and tell it to save and restore its state. // Note that you will often set this in the layout XML, but since // we are sharing our layout with the other fragment we will customize // it here. ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true); return v; } } }