Fragment display for screen display in Android

Source: Internet
Author: User

The demo works as follows:


Also in the vertical screen when the effect is this:

The layout file is as follows:


You can see that there are two resource files, one is to handle the horizontal screen is a vertical screen

The first one:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Match_parent"  Android:orientation="Horizontal" >                <fragmentandroid:id="@+id/titles"android:layout_width="0px"  android:layout_height="Match_parent"android:layout_weight="1"  Class="com.xuliugen.frag.ListFragment" />                                        </linearlayout>

The second one:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  =" match_parent " android:orientation  =;     <fragment  android:id  = "@+id/titles"  android:layout_width  =         "0px"  android:layout_height  = "match_parent"  android:layout_weight  = "1"  class  =/>     <framelayoutandroid:id="@+id/detail"android:layout_width="0px"  android:layout_height="Match_parent"android:layout_weight="2"  Android:background="? android:attr/detailselementbackground" />                                        </linearlayout>
Class Code


Data.java

 Public Final  class Data {    //Title     Public Static FinalString[] TITLES = {"Linear layout","Table Layout","Frame Layout","Relative layout"};//Detailed content     Public Static FinalString[] DETAIL = {a linear layout is where the components placed are laid out in a vertical or horizontal direction, that is, the components in which the controls are placed are arranged horizontally or vertically. "+In a linear layout, only one component can be placed in each row (for vertical arrangement) or in each column (for horizontal arrangement). "+"and the linear layout of Android does not wrap, and when the component is arranged next to the edge of the form, the remaining components will not be displayed." ",The table layout is similar to a common table, which manages the UI components that are placed in rows and columns. "+the table layout uses the <TableLayout> tag definition, and in the table layout you can add multiple <TableRow> tags, "+"Each <TableRow> tag takes one row, and because the <TableRow> tag is also a container, additional components can be added to the tag,"+"in the <TableRow> tag, the table adds a column for each component you add. In a table layout, columns can be hidden, "+"can also be set to stretch to fill the available screen space, or it can be set to force contraction until the table matches the screen size. ","In the frame Layout manager, each component that is added will create a blank area, often called a frame,"+These frames perform automatic alignment based on the Gravity property. By default, the frame layout starts from the upper-left corner (0,0) coordinate point of the screen, and the+"Multiple components are stacked, and later components overwrite the previous components." ",relative layout refers to the layout of a component by its relative position, such as the left, right, top, or bottom of another component. "};}

Detailfragment.java

 PackageCom.xuliugen.frag;ImportAndroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ScrollView;ImportAndroid.widget.TextView; Public  class detailfragment extends Fragment {    //Create a new instance of Detailfragment, including the packets to be passed     Public StaticDetailfragmentnewinstance(intIndex) {detailfragment F =NewDetailfragment ();//Pass index as a parameterBundle bundle =NewBundle ();//instantiation of a bundle objectBundle.putint ("Index", index);//Add the index value to the bundle objectF.setarguments (bundle);//Save the bundle object as a fragment parameter        returnF } Public int Getshownindex() {returnGetarguments (). GetInt ("Index",0);//Gets the list item index to display}@Override     PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {if(Container = =NULL) {return NULL; } ScrollView scroller =NewScrollView (Getactivity ());//Create a scrolling viewTextView Text =NewTextView (Getactivity ());//Create a text box objectText.setpadding (Ten,Ten,Ten,Ten);//Set inner marginScroller.addview (text);//Add a text box object to the scrolling viewText.settext (Data.detail[getshownindex ());//Set the text to be displayed in the text box        returnScroller }}

Listfragment.java

 PackageCom.xuliugen.frag;ImportAndroid.app.FragmentTransaction;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public  class listfragment extends android. app. listfragment {    BooleanDualpane;//Whether the list and details are displayed on one screen at the same time    intCurcheckposition =0;//The index position of the current selection    @Override     Public void onactivitycreated(Bundle savedinstancestate) {Super. onactivitycreated (Savedinstancestate); Setlistadapter (NewArrayadapter<string> (Getactivity (), Android. r.layout.simple_list_item_checked, Data.titles));//Set the adapter for the listView detailframe = getactivity (). Findviewbyid (R.id.detail);//Get the Framelayout frame Layout manager added in the layout fileDualpane = detailframe! =NULL&& detailframe.getvisibility () = = view.visible;//Determine if the list and details are displayed on one screen at the same time        if(Savedinstancestate! =NULL) {curcheckposition = Savedinstancestate.getint ("Curchoice",0);//Update the currently selected index location}if(Dualpane) {//If both the list and details are displayed on a single screenGetlistview (). Setchoicemode (Listview.choice_mode_single);//Set list to radio modeShowDetails (curcheckposition);//Show detailed content}    }//Override Onsaveinstancestate () method to save the index value of the currently selected list item    @Override     Public void onsaveinstancestate(Bundle outstate) {Super. Onsaveinstancestate (Outstate); Outstate.putint ("Curchoice", curcheckposition); }//Override Onlistitemclick () method    @Override     Public void Onlistitemclick(ListView L, View V,intPositionLongID) {showDetails (position);//Call the ShowDetails () method to display the details}voidShowDetails (intIndex) {curcheckposition = index;//Update the value of the variable that holds the current index position is the currently selected value        if(Dualpane) {//When both the list and details are displayed on a single screenGetlistview (). setitemchecked (Index,true);//Set the selected list item to the selected stateDetailfragment details = (detailfragment) Getfragmentmanager (). Findfragmentbyid (R.id.detail);//Get the fragment for displaying detailed content            if(Details = =NULL|| Details.getshownindex ()! = index) {//IfDetails = detailfragment.newinstance (index);//Create a new Detailfragment instance to display the details of the current selection                //To manage fragment in activity, you need to use FragmentmanagerFragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();//Get an example of a fragmenttransactionFt.replace (R.id.detail, details);//Replace the previously displayed detailsFt.settransition (Fragmenttransaction.transit_fragment_fade);//Set conversion effectFt.commit ();//Commit a transaction}        }Else{//When only one of the contents of a list or detail is displayed on a single screen            //Use a new activity to display detailed contentIntent Intent =NewIntent (Getactivity (), MainActivity.DetailActivity.class);//Create a Intent objectIntent.putextra ("Index", index);//Set a parameter to passStartActivity (Intent);//Open a specified activity}    }}

Mainactivity.java

 PackageCom.xuliugen.frag;Importandroid.app.Activity;ImportAndroid.content.res.Configuration;ImportAndroid.os.Bundle; Public  class mainactivity extends Activity {    @Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);    Setcontentview (R.layout.main); }//Create an internal class that inherits activity to display details through activity in the phone interface     Public Static  class detailactivity extends Activity {        @Override        protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//Determine if it is a horizontal screen, and if it is a horizontal screen, end the current activity and prepare to display the details using fragment            if(Getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape) {Finish ();//End Current Activity                return; }if(Savedinstancestate = =NULL) {//                //Insert a fragment that displays detailed content at initialization timeDetailfragment details =NewDetailfragment ();//Instantiate an Detailfragment objectDetails.setarguments (Getintent (). Getextras ());//Set the parameters to be passedGetfragmentmanager (). BeginTransaction (). Add (Android. R.id.content, details). commit ();//Add a fragment that shows detailed content}        }    }}

Fragment display for screen display in Android

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.