Android Api Demos The way to the summit (40) Fragment-->layout
Source: Internet
Author: User
<span id="Label3"></p><p><p>This demo demonstrates the layout features of the Fragment. In this case, in the state of the vertical screen, when we click on the item in the list, an activity is displayed to show the details of this entry.<br>* While on the horizontal screen, because the screen is wide enough, we display two fragment on the interface, one to display the title and the other to display the Details.<br>Layout file to create a vertical screen display in the layout directory: fragment_layout.xml</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-pi"><span class="hljs-pi"><?xml version= "1.0" encoding= "utf-8"?></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">framelayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute"> Android:layout_width</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span> > </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">fragment</span><span class="hljs-attribute">class</span>=<span class="hljs-value">"com.fishtosky.fragmentlayout.mainactivity$titlesframent"</span> <span class="hljs-attribute"> Android:layout_width</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span> <span class="hljs-attribute"> Android:id</span>=<span class="hljs-value">"@+id/titles_fragment"</span>/> </span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">framelayout</span>></span></span></code></pre><p><p>Create the Layout-land folder under the Res directory, where you create a horizontal screen display of the layout file, where you need to be aware that you must have the same name as the layout file under the layouts Directory. Fragment_layout.xml</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-pi"><span class="hljs-pi"><?xml version= "1.0" encoding= "utf-8"?></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">linearlayout </span> <span class="hljs-attribute">xmlns: Android </span> =<span class="hljs-value"> "http://schemas.android.com/apk/res/android" </span> <span class=" Hljs-attribute ">android:layout_width </span> =<span class=" hljs-value ">" match_parent "</span> <span class=" Hljs-attribute ">android:layout_height </span> =<span class=" hljs-value ">" match_parent "</span> <span class=" Hljs-attribute ">android:orientation </span> =<span class=" hljs-value " " horizontal "< span>; </span></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">fragment </span> <span class="hljs-attribute">class </span> =< Span class= "hljs-value" > "com.fishtosky.fragmentlayout.mainactivity$titlesframent" </span> <span class=" Hljs-attribute ">android:layout_width </span> =<span class=" hljs-value ">" 0dp "</span> <span class=" hljs-attribute "> Android:layout_height </span> =<span class="hljs-value"> "match_parent" </span> <span class="hljs-attribute"> Android:id </span> =<span class="hljs-value"> "@+id/titles_fragment" </span> <span class="hljs-attribute">android: Layout_weight </span> =<span class="hljs-value"> "1" </span>/> </span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">framelayout</span><span class="hljs-attribute">android:layout_width</span>= "<span class="hljs-value">0dp"</span><span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">" Match_parent "</span><span class="hljs-attribute">android:layout_weight</span>=<span class="hljs-value">" 2 "</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">" @+id/details_framelayout "</span>> </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">framelayout</span>></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">linearlayout</span>></span></span></code></pre><p><p>Mainactivity</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>string[] title =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>string[] {<span class="hljs-string"><span class="hljs-string">"title1"</span></span>,<span class="hljs-string"><span class="hljs-string">"title2"</span></span>,<span class="hljs-string"><span class="hljs-string">"title3"</span></span>,<span class="hljs-string"><span class="hljs-string">"title4"</span></span>,<span class="hljs-string"><span class="hljs-string">"title5"</span></span>};<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>string[] details =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>string[] {<span class="hljs-string"><span class="hljs-string">"more information about title one"</span></span>,<span class="hljs-string"><span class="hljs-string">"header II details"</span></span>,<span class="hljs-string"><span class="hljs-string">"header three details"</span></span>,<span class="hljs-string"><span class="hljs-string">"header four details"</span></span>,<span class="hljs-string"><span class="hljs-string">"header Five details"</span></span>};<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.fragment_layout); }<span class="hljs-comment"><span class="hljs-comment">//define activity that shows detailed information</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">detialsactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate);<span class="hljs-comment"><span class="hljs-comment">//if It is a horizontal screen, the direct end</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(getresources (). getconfiguration (). orientation = = Configuration.orientation_landscape) {finish ();<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(savedinstancestate==<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {detialsfrtagment df=<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Detialsfrtagment (); Df.setarguments (getintent (). Getextras ()); Getfragmentmanager (). begintransaction (). Add (android. r.id.content, df). Commit (); } } }<span class="hljs-comment">/ <span class="hljs-comment">* Define a class that inherits listfragment to display a list of headings for the content *</span> /</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">titlesframent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">listfragment</span> {</span></span> <span class="hljs-comment"><span class="hljs-comment">//define A Boolean variable that controls whether to display two fragment simultaneously</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span>mdualpane;<span class="hljs-comment"><span class="hljs-comment">//define The ordinal of the currently selected item</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>mcurrentchoiceitem;<span class="hljs-comment">/ <span class="hljs-comment">* * Call this method after the activity has been created</span> */</span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onactivitycreated</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onactivitycreated (savedinstancestate);<span class="hljs-comment"><span class="hljs-comment">//set data Adapter for ListView</span></span>Setlistadapter (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>arrayadapter<string> (getactivity (), Android. r.layout.simple_list_item_activated_1, title));<span class="hljs-comment"><span class="hljs-comment">//find the component to display the detailed information</span></span>framelayout fl = (framelayout) getactivity (). Findviewbyid (r.id.details_framelayout);<span class="hljs-comment"><span class="hljs-comment">/* * To determine if the component is empty and visible, or true to indicate that it is currently in landscape mode, you can display both the title and details two Fragment. If False indicates that the current is in a solid state, only the * title of the fragment can be displayed, by clicking on the title entry to open another activity to display the details *</span> /</span>Mdualpane = fl! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>&& fl.getvisibility () = = view.visible;<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(savedinstancestate! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {mcurrentchoiceitem = Savedinstancestate.getint (<span class="hljs-string"><span class="hljs-string">"currentchoice"</span></span>,<span class="hljs-number"><span class="hljs-number">0</span></span>); }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(mdualpane) {<span class="hljs-comment"><span class="hljs-comment">//set list is a single-selection mode, and when the user selects an entry, the entry will remain highlighted</span></span>Getlistview (). Setchoicemode (listview.choice_mode_single); ShowDetails (mcurrentchoiceitem); } }<span class="hljs-comment">/ <span class="hljs-comment">* * To display the details of the first entry</span> */</span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">showDetails</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Index) {mcurrentchoiceitem = index;<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(mdualpane) {<span class="hljs-comment"><span class="hljs-comment">//set the selected entry to the selected state, rendering highlighting</span></span>Getlistview (). setitemchecked (index,<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>);<span class="hljs-comment"><span class="hljs-comment">//judge The current displayed fragment and replace</span></span>Detialsfrtagment df = (detialsfrtagment) Getfragmentmanager (). Findfragmentbyid (r.id.details_framel ayout);<span class="hljs-comment"><span class="hljs-comment">//if A new entry is selected, or the data is initialized</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(DF = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>|| Df.getshowindex ()! = Index) {detialsfrtagment newfragment = detialsfrtagment . getinstance (mcurrentchoiceitem); Fragmenttransaction ft = Getfragmentmanager (). begintransaction ();<span class="hljs-comment"><span class="hljs-comment">//set Toggle Animation</span></span>Ft.settransition (fragmenttransaction.transit_fragment_fade); Ft.replace (r.id.details_framelayout, newfragment); Ft.commit (); } }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{<span class="hljs-comment"><span class="hljs-comment">//if the title and details windows are not displayed at the same time, a new activity is opened</span></span>Intent Intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (getactivity (), detialsactivity.class); Intent.putextra (<span class="hljs-string"><span class="hljs-string">"index"</span></span>, index); StartActivity (intent); } }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onsaveinstancestate</span></span>(Bundle Outstate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onsaveinstancestate (outstate); Outstate.putint (<span class="hljs-string"><span class="hljs-string">"currentchoice"</span></span>, mcurrentchoiceitem); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Onlistitemclick</span></span>(ListView l, View v,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Position<span class="hljs-keyword"><span class="hljs-keyword">Long</span></span>Id) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. Onlistitemclick (l, v, position, id); ShowDetails (position); } }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">detialsfrtagment</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Fragment</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>Detialsfrtagment<span class="hljs-title"><span class="hljs-title">getinstance</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Index) {detialsfrtagment df =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Detialsfrtagment (); Bundle args =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Bundle (); Args.putint (<span class="hljs-string"><span class="hljs-string">"index"</span></span>, index); Df.setarguments (args);<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>df }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span> <span class="hljs-title"><span class="hljs-title">Getshowindex</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Getarguments (). GetInt (<span class="hljs-string"><span class="hljs-string">"index"</span></span>); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>View<span class="hljs-title"><span class="hljs-title">Oncreateview</span></span>(layoutinflater inflater, viewgroup container, Bundle Savedinstancestate) {<span class="hljs-comment">/ <span class="hljs-comment">* * When the phone is in a horizontal screen, two fragment is displayed normally, and after switching to hard screen mode, the system saves the current * UI information and redraws the Ui. The UI information is restored from savedinstancestate, and Detailfragment * is created, but we do not display the details form in the Hard-screen mode, so we do not need to create the detailfragment at this time * view, It is possible to control whether the view is displayed by container whether it is Empty. */</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(container = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>; }<span class="hljs-comment"><span class="hljs-comment">//execute The following code only where you need to create and return a view</span></span>ScrollView SV =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>ScrollView (getactivity ()); TextView TV =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>TextView (getactivity ());<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>padding = (<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>) Typedvalue.applydimension (typedvalue.complex_unit_dip,<span class="hljs-number"><span class="hljs-number">5</span></span>, getactivity (). getresources (). getdisplaymetrics ()); Tv.setpadding (padding, padding, padding, padding); Sv.addview (tv); Tv.settext (details[getshowindex ());<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Sv } }}</code></pre><p><p>Configure activity</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"></<span class="hljs-title">activity</span>></span> <span class="hljs-tag"><<span class="hljs-title">activity</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.fishtosky.fragmentlayout.MainActivity$DetialsActivity"</span>></span><span class="hljs-tag"></<span class="hljs-title">activity</span>></span></code></pre></pre> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Api Demos The way to the summit (40) Fragment-->layout</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.