Android - Fragments

<p>WML 文档实例:<br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <br /> <card id="HTML" title="HTML Tutorial"></card><br /> </p> <p><br /> Our HTML Tutorial is an award winning<br /> tutorial from W3School.<br /> </p> <p><br /> <br /> <br /> <card id="XML" title="XML Tutorial"></card><br /> </p> <p><br /> Our XML Tutorial is an award winning<br /> tutorial from W3School.<br /> </p> <p><br /> <br /> <br /> <br /> <br /> <br /> <br /> Deck 和 Card<br /> WML 页面通常称为 &quot;deck&quot;。每个 deck 含有一系列的 card。card 元素可包含文本、标记、链接、输入字段、task、图像等等。卡片之间通过链接彼此相互联系。<br /> <br /> 当从移动电话访问一张 WML 页面时,页面中的所有卡片都会从 WAP 服务器下载下来。卡片之间的导航是通过电话的计算机完成的 - 在电话内部 - 不需要对服务器的额外访问:<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <br /> <card id="no1" title="Card 1"></card><br /> </p> <p>Hello World!</p> <p><br /> <br /> <br /> <card id="no2" title="Card 2"></card><br /> </p> <p>Welcome to our WAP Tutorial!</p> <p><br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> 段落和换行<br /> 可以编写显示段落和换行的 WML 卡片:<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <card title="Paragraphs"></card><br /> <br /> </p> <p><br /> This is a paragraph<br /> </p> <p><br /> </p> <p><br /> This is another<br /> with a line break<br /> </p> <p><br /> <br /> <br /> <br /> <br /> <br /> <br /> 文本格式化<br /> 可以编写显示格式化文本的 WML 卡片:<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <card title="Formatting"></card><br /> <br /> </p> <p><br /> normal<br /> <br /> <em>emphasized</em><br /> <br /> <strong>strong</strong><br /> <br /> <b>bold</b><br /> <br /> <i>italic</i><br /> <br /> <u>underline</u><br /> <br /> <big>big</big><br /> <br /> <small>small</small><br /> </p> <p><br /> <br /> <br /> <br /> <br /> <br /> <br /> 链接<br /> 可以制作 WML 卡片来显示 WML 的锚功能。<br /> <br /> <anchor></anchor><br /> <anchor></anchor>标签总是要规定一个任务 (&quot;go&quot;, &quot;prev&quot;, 或 &quot;refresh&quot;)。任务定义了当用户选择此链接时要做的事情。在本例中,当用户选择 &quot;Next page&quot;,其任务是 &quot;前往 test.wml&quot;:<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <card title="Anchor Tag"></card><br /> <br /> </p> <p><br /> <anchor></anchor>Next page<br /> <go href="test.wml"></go><br /> <br /> </p> <p><br /> <br /> <br /> <br /> <br /> <a>标签永远执行 &quot;go&quot; 任务,不带参数。下面的例子与那个 <anchor></anchor>标签的例子起得作用是相同的:<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <card title="A Tag"></card></a><br /> </p> <p><a><br /> </a></p> <p><a> </a></p> <p></p> <p><a href="test.wml">Next page</a><br /> </p> <p><br /> <br /> <br /> <br /> <br /> <br /> 图像<br /> 可以制作 WML 卡片来显示图像:<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <card title="Image"></card><br /> <br /> </p> <p><br /> This is an image<br /> <img alt="stickman" src="/images/stickman.wbmp" /><br /> in a paragraph<br /> </p> <p><br /> <br /> <br /> <br /> <br /> <br /> 任务 (task) 定义了事件发生时所执行的动作,比如在输入一个卡片或选择一个链接之类的时间发生时。<br /> Go 任务<br /> <go></go>任务表示切换到一个新卡片的动作。<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <card></card><br /> </p> <p><br /> <anchor></anchor><br /> Go To Test<br /> <go href="test.wml"></go><br /> <br /> </p> <p><br /> <br /> <br /> <br /> <br /> Prev 任务<br /> <prev></prev> 任务表示后退到前面的卡片的动作。<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <card></card><br /> </p> <p><br /> <anchor></anchor><br /> Previous Page<br /> <prev></prev><br /> <br /> </p> <p><br /> <br /> <br /> <br /> <br /> <br /> Refresh 任务<br /> <refresh></refresh>任务会刷新一些指定的卡片变量。如果变量显示在屏幕上,任务也会刷新屏幕。<br /> <br /> 下面的例子使用 <anchor></anchor>标签向卡片添加了一个 &quot;Refresh this page&quot; 链接。当用户点击该链接时,他或她会刷新页面,同时变量 x 的值将被设置为 30:<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <card></card><br /> </p> <p><br /> <anchor></anchor><br /> Refresh this page<br /> <go href="thispage.wml"></go><br /> <refresh></refresh><br /> <setvar name="x" value="30"></setvar><br /> <br /> <br /> </p> <p><br /> <br /> <br /> <br /> <br /> <br /> Noop 任务<br /> <noop></noop>任务规定不做任何事(noop 指的是 &quot;no operation&quot;)。此标签用于覆盖卡片组级别的元素。<br /> <br /> <do></do>标签用于在用户点击屏幕上的某个单词或短语时启动一个任务。<br /> <br /> 下面的例子使用 <do></do>标签向卡片添加一个 &quot;Back&quot; 链接。当用户点击这个 &quot;Back&quot; 链接时,他或他被被带回前面的卡片。但是 <noop></noop>标签阻止了这个操作;当用户点击 &quot;Back&quot; 时,不会发生任何事情:<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <card></card><br /> </p> <p><br /> <do label="Back" name="back" type="prev"></do><br /> <noop></noop><br /> <br /> </p> <p><br /> <br /> <br /> <br /> <br /> 带有两个卡片的一个卡片组 - 一个供用户输入,一个供显示结果 - 正如本例中展示的:<br /> <br /> <!--l version="1.0--><br /> &quot;http://www.wapforum.org/DTD/wml_1.1.xml&quot;&gt;<br /> <br /> <wml></wml><br /> <br /> <card id="card1" title="Tutorial"></card><br /> <do label="Answer" type="accept"></do><br /> <go href="#card2"></go><br /> <br /> </p> <p><br /> <select name="name"><option selected="selected" value="HTML">HTML Tutorial</option><option value="XML">XML Tutorial</option><option value="WAP">WAP Tutorial</option> </select><br /> </p> <p><br /> <br /> <br /> <card id="card2" title="Answer"></card><br /> </p> <blockquote >A<b >Fragment</b>is a piece of an activity which enable more modular activity design. It will not be wrong if we say, a fragment is a kind of<b >sub-activity</b>.</blockquote> <p >Following are important points about fragment &minus;</p> <ul class="list" > <li > <p >A fragment has its own layout and its own behaviour with its own life cycle callbacks.</p> </li> <li > <p >You can add or remove fragments in an activity while the activity is running.</p> </li> <li > <p >You can combine multiple fragments in a single activity to build a multi-plane UI.</p> </li> <li > <p >A fragment can be used in multiple activities.</p> </li> <li > <p >Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped.</p> </li> <li > <p >A fragment can implement a behaviour that has no user interface component.</p> </li> <li > <p >Fragments were added to the Android API in Honeycomb version of Android which API version 11.</p> </li> </ul> <p >You create fragments by extending<b >Fragment</b>class and You can insert a fragment into your activity layout by declaring the fragment in the activity&#39;s layout file, as a<b >&lt;fragment&gt;</b>element.</p> <p >Prior to fragment introduction, we had a limitation because we can show only a single activity on the screen at one given point in time. So we were not able to divide device screen and control different parts separately. But with the introduction of fragment we got more flexibility and removed the limitation of having a single activity on the screen at a time. Now we can have a single activity but each activity can comprise of multiple fragments which will have their own layout, events and complete life cycle.</p> <p >Following is a typical example of how two UI modules defined by fragments can be combined into one activity for a tablet design, but separated for a handset design.</p> <p><img alt="Android Fragment" src="http://www.tutorialspoint.com/android/images/android_fragments.jpg" /></p> <p >The application can embed two fragments in Activity A, when running on a tablet-sized device. However, on a handset-sized screen, there&#39;s not enough room for both fragments, so Activity A includes only the fragment for the list of articles, and when the user selects an article, it starts Activity B, which includes the second fragment to read the article.</p> <h2 >Fragment Life Cycle</h2> <p >Android fragments have their own life cycle very similar to an android activity. This section briefs different stages of its life cycle.</p> <p><img alt="Fragment" src="http://www.tutorialspoint.com/android/images/fragment.jpg" /></p> <h4 align="Center" >FRAGMENT LIFECYCLE</h4> <p >Here is the list of methods which you can to override in your fragment class &minus;</p> <ul class="list" > <li > <p ><b >onAttach()</b>The fragment instance is associated with an activity instance.The fragment and the activity is not fully initialized. Typically you get in this method a reference to the activity which uses the fragment for further initialization work.</p> </li> <li > <p ><b >onCreate()</b>The system calls this method when creating the fragment. You should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.</p> </li> <li > <p ><b >onCreateView()</b>The system calls this callback when it&#39;s time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a<b >View</b>component from this method that is the root of your fragment&#39;s layout. You can return null if the fragment does not provide a UI.</p> </li> <li > <p ><b >onActivityCreated()</b>The onActivityCreated() is called after the onCreateView() method when the host activity is created. Activity and fragment instance have been created as well as the view hierarchy of the activity. At this point, view can be accessed with the findViewById() method. example. In this method you can instantiate objects which require a Context object</p> </li> <li > <p ><b >onStart()</b>The onStart() method is called once the fragment gets visible.</p> </li> <li > <p ><b >onResume()</b>Fragment becomes active.</p> </li> <li > <p ><b >onPause()</b>The system calls this method as the first indication that the user is leaving the fragment. This is usually where you should commit any changes that should be persisted beyond the current user session.</p> </li> <li > <p ><b >onStop()</b>Fragment going to be stopped by calling onStop()</p> </li> <li > <p ><b >onDestroyView()</b>Fragment view will destroy after call this method</p> </li> <li > <p ><b >onDestroy()</b>onDestroy() called to do final clean up of the fragment&#39;s state but Not guaranteed to be called by the Android platform.</p> </li> </ul> <h2 >How to use Fragments?</h2> <p >This involves number of simple steps to create Fragments.</p> <ul class="list" > <li > <p >First of all decide how many fragments you want to use in an activity. For example let&#39;s we want to use two fragments to handle landscape and portrait modes of the device.</p> </li> <li > <p >Next based on number of fragments, create classes which will extend the<i >Fragment</i>class. The Fragment class has above mentioned callback functions. You can override any of the functions based on your requirements.</p> </li> <li > <p >Corresponding to each fragment, you will need to create layout files in XML file. These files will have layout for the defined fragments.</p> </li> <li > <p >Finally modify activity file to define the actual logic of replacing fragments based on your requirement.</p> </li> </ul> <h2 >Types of Fragments</h2> <p >Basically fragments are divided as three stages as shown below.</p> <ul class="list" > <li > <p ><a href="http://www.tutorialspoint.com/android/android_single_fragments.htm" >Single frame fragments</a>&minus; Single frame fragments are using for hand hold devices like mobiles, here we can show only one fragment as a view.</p> </li> <li > <p ><a href="http://www.tutorialspoint.com/android/android_list_fragment.htm" >List fragments</a>&minus; fragments having special list view is called as list fragment</p> </li> <li > <p ><a href="http://www.tutorialspoint.com/android/android_fragment_transitions.htm" >Fragments transaction</a>&minus; Using with fragment transaction. we can move one fragment to another fragment.</p> </li> </ul> <p><br /> <br /> </p>
RangeTime:0.009327s
RangeMem:219.53 KB
返回顶部 留言