android6.0官方教程笔记——Building a Dymanic UI with Fragmemts-1
2016-12-15 10:03
507 查看
Creating a Fragment
introduce:
You can think of a fragment as a modular section of an activity, which has its own lifecycle, 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).
Ⅰ.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:
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 to onPause().
Ⅱ.Add a Fragment to an Activity using XML
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.
Here is an example layout file that adds two fragments to an activity when the device screen is considered "large" (specified by the large qualifier in the directory name).
Then apply the layout to your activity:
Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot 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
本文原创发布于慕课网 ,转载请注明出处,谢谢合作!
作者: niiiiiiiiiii
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作!
introduce:
You can think of a fragment as a modular section of an activity, which has its own lifecycle, 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).
Ⅰ.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 to onPause().
Ⅱ.Add a Fragment to an Activity using XML
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.
Here is an example layout file that adds two fragments to an activity when the device screen is considered "large" (specified by the large qualifier in the directory name).
<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>
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); } }(<a href="http://www.dztcsd.com/">资质代办</a>)
Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot 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
本文原创发布于慕课网 ,转载请注明出处,谢谢合作!
作者: niiiiiiiiiii
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作!
相关文章推荐
- Android官方Training阅读笔记 ---- Building a Dynamic UI with Fragments(Creating a Fragment) (一)
- Building a Dynamic UI with Fragments ---- Communicating with Other Fragme (三)
- android开发官方教程之Building a Dynamic UI with Fragments
- Android官方Training阅读笔记 ---- Building a Dynamic UI with Fragments(Building a Flexible UI) (二)
- google 官方教程学习之UI性能优化( Analyzing UI Performance with Systrace)
- Building a Dynamic UI with Fragments笔记
- TensorFlow官方教程学习笔记之1-TensorFlow入门教程(Getting Started With TensorFlow)
- Training—Building a Dynamic UI with Fragments
- WinCE Application: Building UI components with dynamic visual effect
- Building a Dynamic UI with Fragments
- 详解 “Android UI”设计官方教程
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之使用Support Library
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之创建一个Fragment
- 阮一峰:jQuery官方基础教程笔记
- Building a dynamic UI with fragments
- 谷歌官方教程:Android UI设计技巧
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之创建一个灵活的UI
- jQuery官方基础教程笔记
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之创建一个Fragment
- 多图详解 “Android UI”设计官方教程(一)