Introducing Fragments

  • View
    3.738

  • Download
    7

Embed Size (px)

Transcript

  • 1. Motorola Mobility Anna Schaller Android Fragment

2. Fragment

  • ( : UI ).

3. Fragment ?

  • .
  • Fragment

Fragment Activity A 4. (Getting there)

    • Android 3.0 SDK
    • Android Compatibility( )
    • +
  • Activity
  • Fragment
  • Fragment UI
  • Fragment Activity

5.

    • , , ,
  • 2
    • /res/layout-small-land/res/layout-small-port
    • /res/layout-normal-land/res/layout-normal-port
    • /res/layout-large-land/res/layout-large-port
    • /res/layout-xlarge-land/res/layout-xlarge-land

6. ( ) 7. Fragment Fragment Fragment Activity findFragmentById(R.id. DetailFragment ) setContentView(R.layout. main ) 8. Android 3.0 Fragment

  • Fragment
    • android.app.Fragment ;
    • FragmentListFragment (Activity )
    • (Lifecycle)
  • Fragment resource.xml
  • Fragment Fragmentmain.xml
    • android:id=@+id/fragmentIdX>
  • MainApp.java (Activity )
    • setContentView(R.layout.main)
  • Fragment1 findFragmentById(R.id.fragmentIdX) Fragment2 .

9. (Android 3.0)

  • myFragmentProject
  • |-- AndroidManifest.xml
  • |-- /src
  • |------- /main.java // Activity
  • |------- /fragment1.java // Fragment, ListFragment, DialogFragment
  • |------- /fragment2.java // Fragment, ListFragment, DialogFragment
  • |--- /res
  • |------- /layout-land //
  • |----------- /main.xml// (Fragment )
  • |----------- /fragment1.xml // Fragment 1 UI
  • |----------- /fragment2.xml // Fragment 2 UI
  • |------- /layout-port //
  • |----------- /main.xml // (Fragment )
  • |----------- /fragment2.xml // Fragment 2 UI
  • |------- /values-xlarge
  • | . . .

10. 3.0 :main.java

  • Activity onCreate()
  • setContentView( R.layout.main ) .Fragment Activity .

11. 3.0 : fragment1( ) fragment2( ) 12. 3.0 : 13. 3.0 :Fragment Activity Fragment 14. 3.0 :Fragment 15. 3.0 : Fragment UI (fragment1) (fragment2) (ImageView ) 16.

  • UI
    • Activity (View)

UI Fragment listfragment.xml fragment.xml [. . .] dialogfragment 17. UI Fragment 18. findFragmentByTag()

  • public static classWorkerFragmentextends Fragment {
  • [. . .]//
  • }
  • public static class myOtherFragment extends Fragment {
  • @Override
  • public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState);
  • FragmentManager fm =getFragmentManager () ; //Fragment . mWorkFragment = ( WorkerFragment ) fm. findFragmentByTag ("work") ; // ( ) . if(mWorkFragment == null) { mWorkFragment = newWorkerFragment ();
  • // . mWorkFragment.setTargetFragment(this, 0); fm.beginTransaction().add(mWorkFragment, "work").commit();
  • }
  • }
  • }

19. Android Compatibility( )

    • Fragment
    • Honeycomb Android Compatibility( )
    • ?
      • Compatibility Library( ) API4
      • Activity
      • API

20. (Android 2.2( )) 21. (Android 2.2( )) 22. Fragment Fragment Fragment FragmentActivity1 findFragmentById(R.id. DetailFragment ) setContentView(R.layout. main ) 23. Fragment Fragment Fragment Activity1 Fragment Activity2 Fragment setContentView(R.layout. main ) findViewById(R.id. DetailFragment )is NULL intent.setClass(getActivity(),Fragment2. class); startActivity(intent ); 24. Compatibility Library( )

    • fragment1 (fragment2 )
    • android.v4.support .*
    • boolean
    • (orientation == landscape)
      • fragment1fragment2
    • (orientation == portrait)
      • intent FragmentActivity fragment2
    • getFragmentManager() getSupportFragmentManager()

25. + (Android Compatibility )

  • myFragmentProject
  • |-- AndroidManifest.xml
  • |-- /src
  • |------- /main.java // FragmentActivity
  • |------- /fragment1.java // Fragment, ListFragment, DialogFragment
  • |------- /fragment2.java // Fragment, ListFragment, DialogFragment
  • |------- /fragment2Main.java // FragmentActivity
  • |--- /res
  • |------- /layout-land // ( )
  • |----------- /main.xml// ( )
  • |----------- /fragment1.xml // Fragment1 UI
  • |----------- /fragment2.xml // Fragment2 UI
  • |------- /layout-port // ( )
  • |----------- /main.xml // ( )
  • |----------- /fragment1.xml
  • |------- /values-xlarge
  • | . . .

26. Android Compatibility( )

    • Android SDKAVD Manager Android Compatibility Package( ) . SDK extras/android/compatibility/v4/android-support-v4.jar .
    • API (4-10) Android Project( ) .
    • android-support-v4.jar /libs .
    • StudioEclipse (Project -> Properties -> Java Build Path -> Libraries -> Add JAR).

27. Fragment :

  • 3.0 Developer Guide Fragment
  • 3.0 Reference for APIs
    • android.app.Fragment
    • android.app.ListFragment
      • ListActivity
    • android.app.DialogFragment
    • android.app.FragmentManager
      • Fragment ( findFragmentById)
    • android.app.FragmentTransaction
      • Fragment API( add, remove, replace, hide, show)

28. Fragment :

  • Android 3.0 Fragment Honeycomb Gallery
  • Android Compatibilityextra
    • /android-sdk/extras/android/compatibility/v4/samples/ApiDemos/src/com/example/android/apis/app/
    • HCFragment.java
    • Android Compatibility FragmentSupport*.java
  • Reto Meier
    • http://blog.radioactiveyak.com/2011/02/android-app-surgery-earthquake-redux.html
    • http://blog.radioactiveyak.com/2011/02/strategies-for-honeycomb-and-backwards.html
  • Diane Hackborne
    • http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html (Android 3.0 Fragment API)

29. : developer.motorola.com

    • http://community.developer.motorola.com/
    • MOTODEV Studio: ( , , ) IDE
    • App Validator: Android
  • (Twitter)
    • appsum11kr
    • http://developer.motorola.com/products/xoom/

30. 2010 Motorola Mobility, Inc. 46 31.

  • Motorola Mobility Inc. .
  • Copyright 2010-2011, Motorola, Inc. All rights reserved( ).
  • 2 .
  • , .
  • 2 , / .
  • Motorola, Inc. .
  • " " ( ) . , , , , ( , , , ) , ( ) ,
  • .
  • Apache 2.0
  • Copyright 2010, Android Open Source Project. All rights reserved( ).
  • Apache ,2.0(" "); .http://www.apache.org/licenses/LICENSE-2.0 .
  • " " . .
  • Creative Commons 3.0
  • Google (http://code.google.com/policies.html) Creative Commons 3.0 (http://creativecommons.org/licenses/by/3.0/) .