7. Анатомия - Home icon - Title - Navigation - Custom View placeholder - Action buttons
8. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id. home : // app icon in Action Bar clicked; go home Intent intent = new Intent( this , HomeActivity . class ); intent.addFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP ); startActivity(intent); return true ; default : return super .onOptionsItemSelected(item); } } Home icon
9. ActionBar actionBar = this .getActionBar(); actionBar. setDisplayHomeAsUpEnabled( true ) ; Home as “Up”
19. <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "horizontal" android:layout_width = " match_parent " android:layout_height = " match_parent " > < fragment android:name = "com.example.news.ArticleListFragment" android:id = "@+id/list" android:layout_weight = "1" android:layout_width = "0dp" android:layout_height = " match_parent " /> < fragment android:name = "com.example.news.ArticleReaderFragment" android:id = "@+id/viewer" android:layout_weight = "2" android:layout_width = "0dp" android:layout_height = " match_parent " /> </ LinearLayout > Add a Fragment in XML
20. FragmentManager fragmentManager = getFragmentManager( ); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragment fragment = new ExampleFragment (); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit(); Add a Fragment in code
21. // Create new fragment and transaction Fragment newFragment = new ExampleFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack( null ); // Commit the transaction transaction.commit(); Fragment transactions
22. Communicating with the Activity View listView = getActivity().findViewById(R.id. list); Tab2Fragment fragment = (Tab2Fragment) getFragmentManager().findFragmentById(R.id.tab2); From Fragment to Activity From Activity (of another Fragment) to Fragment Tab2Fragment fragment = (Tab2Fragment) getFragmentManager().findFragmentByTag( "tab2" );
23. Custom action buttons for Fragment public class Tab2Fragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle state) { View view = inflater.inflate(R.layout. tab2 , null ); setHasOptionsMenu( true ); return view; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu. tab2_menu , menu); } }
24. Фрагменты доступны с версии Android 1.6 (API level 4) при использовании пакета совместимости, который входит в Android SDK (android-support-v4.jar)
Android introduced fragments in Android 3.0 (API Level &quot;Honeycomb&quot;), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet's screen is much larger than that of a mobile phone, there's more room to combine and interchange UI components A fragment should be a modular and reusable component in your application.