1 /* 2 * Copyright (C) 2010 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.Shakespeare; 20 import com.example.android.supportv4.R; 21 22 import android.support.v4.app.Fragment; 23 import android.support.v4.app.FragmentActivity; 24 import android.support.v4.app.FragmentTransaction; 25 import android.support.v4.app.ListFragment; 26 27 import android.content.Intent; 28 import android.content.res.Configuration; 29 import android.os.Bundle; 30 import android.util.TypedValue; 31 import android.view.LayoutInflater; 32 import android.view.View; 33 import android.view.ViewGroup; 34 import android.widget.ArrayAdapter; 35 import android.widget.ListView; 36 import android.widget.ScrollView; 37 import android.widget.TextView; 38 39 /** 40 * Demonstration of using fragments to implement different activity layouts. 41 * This sample provides a different layout (and activity flow) when run in 42 * landscape. 43 */ 44 public class FragmentLayoutSupport extends FragmentActivity { 45 46 //BEGIN_INCLUDE(main) 47 @Override 48 protected void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 51 setContentView(R.layout.fragment_layout_support); 52 } 53 //END_INCLUDE(main) 54 55 /** 56 * This is a secondary activity, to show what the user has selected 57 * when the screen is not large enough to show it all in one activity. 58 */ 59 //BEGIN_INCLUDE(details_activity) 60 public static class DetailsActivity extends FragmentActivity { 61 62 @Override 63 protected void onCreate(Bundle savedInstanceState) { 64 super.onCreate(savedInstanceState); 65 66 if (getResources().getConfiguration().orientation 67 == Configuration.ORIENTATION_LANDSCAPE) { 68 // If the screen is now in landscape mode, we can show the 69 // dialog in-line with the list so we don't need this activity. 70 finish(); 71 return; 72 } 73 74 if (savedInstanceState == null) { 75 // During initial setup, plug in the details fragment. 76 DetailsFragment details = new DetailsFragment(); 77 details.setArguments(getIntent().getExtras()); 78 getSupportFragmentManager().beginTransaction().add( 79 android.R.id.content, details).commit(); 80 } 81 } 82 } 83 //END_INCLUDE(details_activity) 84 85 /** 86 * This is the "top-level" fragment, showing a list of items that the 87 * user can pick. Upon picking an item, it takes care of displaying the 88 * data to the user as appropriate based on the currrent UI layout. 89 */ 90 //BEGIN_INCLUDE(titles) 91 public static class TitlesFragment extends ListFragment { 92 boolean mDualPane; 93 int mCurCheckPosition = 0; 94 95 @Override 96 public void onActivityCreated(Bundle savedInstanceState) { 97 super.onActivityCreated(savedInstanceState); 98 99 // Populate list with our static array of titles. 100 setListAdapter(new ArrayAdapter<String>(getActivity(), 101 R.layout.simple_list_item_checkable_1, 102 android.R.id.text1, Shakespeare.TITLES)); 103 104 // Check to see if we have a frame in which to embed the details 105 // fragment directly in the containing UI. 106 View detailsFrame = getActivity().findViewById(R.id.details); 107 mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE; 108 109 if (savedInstanceState != null) { 110 // Restore last state for checked position. 111 mCurCheckPosition = savedInstanceState.getInt("curChoice", 0); 112 } 113 114 if (mDualPane) { 115 // In dual-pane mode, the list view highlights the selected item. 116 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 117 // Make sure our UI is in the correct state. 118 showDetails(mCurCheckPosition); 119 } 120 } 121 122 @Override 123 public void onSaveInstanceState(Bundle outState) { 124 super.onSaveInstanceState(outState); 125 outState.putInt("curChoice", mCurCheckPosition); 126 } 127 128 @Override 129 public void onListItemClick(ListView l, View v, int position, long id) { 130 showDetails(position); 131 } 132 133 /** 134 * Helper function to show the details of a selected item, either by 135 * displaying a fragment in-place in the current UI, or starting a 136 * whole new activity in which it is displayed. 137 */ 138 void showDetails(int index) { 139 mCurCheckPosition = index; 140 141 if (mDualPane) { 142 // We can display everything in-place with fragments, so update 143 // the list to highlight the selected item and show the data. 144 getListView().setItemChecked(index, true); 145 146 // Check what fragment is currently shown, replace if needed. 147 DetailsFragment details = (DetailsFragment) 148 getFragmentManager().findFragmentById(R.id.details); 149 if (details == null || details.getShownIndex() != index) { 150 // Make new fragment to show this selection. 151 details = DetailsFragment.newInstance(index); 152 153 // Execute a transaction, replacing any existing fragment 154 // with this one inside the frame. 155 FragmentTransaction ft = getFragmentManager().beginTransaction(); 156 if (index == 0) { 157 ft.replace(R.id.details, details); 158 } else { 159 ft.replace(R.id.a_item, details); 160 } 161 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 162 ft.commit(); 163 } 164 165 } else { 166 // Otherwise we need to launch a new activity to display 167 // the dialog fragment with selected text. 168 Intent intent = new Intent(); 169 intent.setClass(getActivity(), DetailsActivity.class); 170 intent.putExtra("index", index); 171 startActivity(intent); 172 } 173 } 174 } 175 //END_INCLUDE(titles) 176 177 /** 178 * This is the secondary fragment, displaying the details of a particular 179 * item. 180 */ 181 //BEGIN_INCLUDE(details) 182 public static class DetailsFragment extends Fragment { 183 /** 184 * Create a new instance of DetailsFragment, initialized to 185 * show the text at 'index'. 186 */ 187 public static DetailsFragment newInstance(int index) { 188 DetailsFragment f = new DetailsFragment(); 189 190 // Supply index input as an argument. 191 Bundle args = new Bundle(); 192 args.putInt("index", index); 193 f.setArguments(args); 194 195 return f; 196 } 197 198 public int getShownIndex() { 199 return getArguments().getInt("index", 0); 200 } 201 202 @Override 203 public View onCreateView(LayoutInflater inflater, ViewGroup container, 204 Bundle savedInstanceState) { 205 if (container == null) { 206 // We have different layouts, and in one of them this 207 // fragment's containing frame doesn't exist. The fragment 208 // may still be created from its saved state, but there is 209 // no reason to try to create its view hierarchy because it 210 // won't be displayed. Note this is not needed -- we could 211 // just run the code below, where we would create and return 212 // the view hierarchy; it would just never be used. 213 return null; 214 } 215 216 ScrollView scroller = new ScrollView(getActivity()); 217 TextView text = new TextView(getActivity()); 218 int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 219 4, getActivity().getResources().getDisplayMetrics()); 220 text.setPadding(padding, padding, padding, padding); 221 scroller.addView(text); 222 text.setText(Shakespeare.DIALOGUE[getShownIndex()]); 223 return scroller; 224 } 225 } 226 //END_INCLUDE(details) 227 } 228