开发者

Fragments in Android 2.2.1, 2.3, 2.0. Is this possible?

Basically I would like to know if we can have fragment layouts in devices with Android OS < 3.0.

My app had a header on top with 5 different buttons and on start always the first button is clicked by default so the view below these buttons is for the first view. Now when you click on the second button 开发者_JAVA技巧beside it, I don't want the header images to be refreshed but the view just below it needs to be refreshed. So its like updating the fragments below the header image buttons.

So can we have fragments in Android in devices with OS < 3.0.

Sana.


You have to use the compatibility libraries provided by Google. Here's how you use Fragments on devices < 3.0

  • Open Eclipse
  • Window->Android SDK and AVD
  • Available Packages->Android Support package (install this)

Once installed, right click the Android project you want to add Fragment support for.

  • Build Path->Configure Build Path
  • Libraries tab
  • Add External JARs
  • Add the android-support-v4.jar (should be in the android downloads folder under extras/android/support/v4

Now you application supports Fragments. There are some key differences to using the compatibility package over using SDK 3.0+. For instance

  1. The activity classes that use fragments must extend FragmentActivity NOT Activity.
  2. instead of getFragmentManager() you have to use getSupportFragmentManager

Enjoy!!!


Yes, fragments are supported from Android 1.6. For more information see: Compatibility Library.


In Eclipse Indigo, you can right click on the project --> Android Tools --> Add Support Library. Then, instead of using import android.app.Fragment for OS>3.0, use import android.support.v4.app.Fragment;


For Android Studio you need to right click on the app name>Open module settings>Dependencies tab>click on '+' to add the dependency.

You need to add this in all your fragments:

import android.support.v4.app.Fragment;

import this to your MainActivity:

Use this import android.support.v4.app.FragmentManager; instead of import android.app.FragmentManager;

And instead of getFragmentManager() you have to use getSupportFragmentManager();.


yes, Android provide provide support library for the backward compatibility. select the project-> right click->android tools->add support library

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜