<Android developer> Creating a Fragment
来源:互联网 发布:创业数据库 编辑:程序博客网 时间:2024/05/21 08:02
You can think of a fragment as a modular section of an activity, which has its own life cycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities). This lesson shows how to extend the Fragment
class using the Support Library so your app remains compatible with devices running system versions as low as Android 1.6.
Note: If you decide that the minimum API level your app requires is 11 or higher, you don't need to use the Support Library and can instead use the framework's built in Fragment
class and related APIs. Just be aware that this lesson is focused on using the APIs from the Support Library, which use a specific package signature and sometimes slightly different API names than the versions included in the platform.
Before you begin this lesson, you must set up your Android project to use the Support Library. If you have not used the Support Library before, set up your project to use the v4 library by following the Support Library Setup document. However, you can also include the action bar in your activities by instead using the v7 appcompat library, which is compatible with Android 2.1 (API level 7) and also includes the Fragment
APIs.
Create a Fragment Class
To create a fragment, extend the Fragment
class, then override key lifecycle methods to insert your app logic, similar to the way you would with an Activity
class.
One difference when creating a Fragment
is that you must use the onCreateView()
callback to define the layout. In fact, this is the only callback you need in order to get a fragment running. For example, here's a simple fragment that specifies its own layout:
import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.ViewGroup;public class ArticleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.article_view, container, false); }}
Just like an activity, a fragment should implement other lifecycle callbacks that allow you to manage its state as it is added or removed from the activity and as the activity transitions between its lifecycle states. For instance, when the activity's onPause()
method is called, any fragments in the activity also receive a call toonPause()
.
More information about the fragment lifecycle and callback methods is available in the Fragments developer guide.
While fragments are reusable, modular UI components, each instance of a Fragment
class must be associated with a parent FragmentActivity
. You can achieve this association by defining each fragment within your activity layout XML file.
Note: FragmentActivity
is a special activity provided in the Support Library to handle fragments on system versions older than API level 11. If the lowest system version you support is API level 11 or higher, then you can use a regular Activity
.
large
qualifier in the directory name).res/layout-large/news_articles.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.example.android.fragments.HeadlinesFragment" android:id="@+id/headlines_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.android.fragments.ArticleFragment" android:id="@+id/article_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /></LinearLayout>Tip: For more about creating layouts for different screen sizes, read Supporting Different Screen Sizes.
Then apply the layout to your activity:
import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); }}If you're using the v7 appcompat library, your activity should instead extend
ActionBarActivity
, which is a subclass of FragmentActivity
(for more information, read Adding the Action Bar).Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, youcannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts, as shown in the next lesson.
- <Android developer> Creating a Fragment
- Creating a Fragment
- Creating a Fragment
- 09Creating a Fragment
- 《Training:Creating a Fragment》
- 创建一个内容提供者Creating a Content Provider——翻译总结自developer.android.com
- Android官方Training阅读笔记 ---- Building a Dynamic UI with Fragments(Creating a Fragment) (一)
- 安卓初学-创建Fragment(Creating a Fragment)
- Android-Creating a Bound Service
- android-Creating a Content Provider
- android-Creating a Search Interface
- android-Creating a Navigation Drawer
- Building a Dynamic UI with Fragments - Creating a Fragment
- android中Creating a Searchable Activity
- Android: Creating a “Snapping” Horizontal Scroll View
- Android: creating two columns in a linearlayout
- android-Creating a Content Provider(2)
- Android TV -2.1- Creating a Catalog Browser
- andorid开源快速开发框架之AndroidAnnoatitions
- Linux输入子系统:事件的编码 -- event-codes.txt
- dojo小例子(25)在循环结束后再执行后续语句
- github ssh 设置
- python 编码规范及习惯写法范例
- <Android developer> Creating a Fragment
- 关于cocos2dx分数的存储
- windows下编译ffmpeg
- 通讯录调用微信客户端查看好友朋友圈与聊天
- 二分查找
- HDU4965Fast Matrix Calculation(2014多校第九场)
- Linux输入子系统:输入设备编程指南 -- input-programming.txt .
- IOS -- ViewController的LoadView和ViewDidLoad方法相关知识总结
- 【杂题】 HDOJ 4961 Boring Sum