Разработка приложений для android honeycomb: actionbar & fragments

Download Разработка приложений для Android Honeycomb: ActionBar & Fragments

If you can't read please download the document

Upload: alexey-ustenko

Post on 16-Jun-2015

1.968 views

Category:

Technology


4 download

TRANSCRIPT

  • 1. Alexey Ustenko programmer @ustav

2. Android Honeycomb: ActionBar & Fragments 3. ActionBar 4. 5. 6. < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.helloworld" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "4" android:targetSdkVersion = "11" /> < application . ..> ... application > manifest > ActionBar 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 : //appicon in Action Bar clicked; go home Intent intent =newIntent( 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 10. NAVIGATION_MODE_LIST NAVIGATION_MODE_TABS Navigation Mode 11. < menu xmlns:android = "http://schemas.android.com/apk/res/android" > < item android:id = "@+id/menu_search" android:title = "Search" android:icon = "@drawable/ic_menu_search" android:showAsAction = "ifRoom" android:actionLayout = "@layout/searchview" /> menu > < menu xmlns:android = "http://schemas.android.com/apk/res/android" > < item android:id = "@+id/menu_search" android:title = "Search" android:icon = "@drawable/ic_menu_search" android:showAsAction = "ifRoom" android:actionViewClass = "android.widget.SearchView" /> menu > Action Layout 12. Action Mode 13. getActivity().startActionMode ( newCallback() { @Override public booleanonPrepareActionMode(ActionMode mode, Menu menu) { return false ; } @Override public voidonDestroyActionMode(ActionMode mode) { } @Override public booleanonCreateActionMode(ActionMode mode, Menu menu) { return false ; } @Override public booleanonActionItemClicked(ActionMode mode, MenuItem item) { return false ; } }); Action Mode code example 14. Fragments 15. 16. DialogFragment 17. ListFragment 18. PreferenceFragment 19. < 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(); ExampleFragmentfragment =new ExampleFragment (); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit(); Add a Fragment in code 21. // Create new fragment and transaction Fragment newFragment =newExampleFragment(); 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 classTab2FragmentextendsFragment { @Override publicView onCreateView(LayoutInflater inflater, ViewGroup parent,Bundle state) { View view = inflater.inflate(R.layout. tab2 ,null ); setHasOptionsMenu( true ); returnview; } @Override public voidonCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu. tab2_menu , menu); } } 24. Android 1.6 (API level 4) , Android SDK (android-support-v4.jar) 25. http://developer.android.com