Fragment How to create
There are two ways to use fragment: Static mode and dynamic mode.
1. Static mode
First step: Define a fragment subclass first.
public class Examplefragment extends Fragment {
@Override public
View Oncreateview (Layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {return
inflater.inflate (r.layout.example_fragment, container, False);
}
Description: Examplefragment is a subclass of fragment, and its layout definition is Example_fragment.xml file.
Step Two: Define the layout file for the fragment subclass.
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
xmlns:tools=" Http://schemas.android.com/tools "
android:layout_width=" Match_parent
" android:layout_height= "Match_parent"
android:orientation= "Horizontal" >
<edittext android:id= "@+id" /edit_message "
android:layout_width=" 0DP "
android:layout_height=" wrap_content "
android:layout_ weight= "1"
android:hint= "@string/edit_message"/>
<button android:layout_width=
"Wrap_ Content "
android:layout_height=" wrap_content "
android:text=" "@string/button_send"
android:o nclick= "SendMessage"/>
</LinearLayout>
Description: Above is the content of Example_fragment.xml.
Step three: Use the fragment in the layout that needs to be used for the fragment activity.
Here's the code that references the fragment activity:
The public class Fragmentlayouttest extends activity {/** called the ' when ' is the ' The activity ' is the ' the '
---'
@Override Public
void OnCreate (Bundle savedinstancestate)
{
super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
Here's what the Main.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent"
>
<textview
android:layout_width= "fill_parent"
android:layout_ height= "Wrap_content"
android:text= "@string/fragment_intro"
/> <fragment android:name=
" Com.skw.fragmentlayouttest.ExampleFragment "
android:id=" @+id/frag_example "
android:layout_weight=" 1 "
android:layout_width= "match_parent"
android:layout_height= "match_parent"/>
</ Linearlayout>
Description: In this layout file, a previously customized examplefragment is called.
Click to view: Static mode full source
2. Dynamic mode
repeat the first and second steps above to implement a fragment subclass.
Step three: Define a framelayout in the layout that needs to be used for the fragment activity.
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent"
>
<textview
android:layout_width= "fill_parent"
android:layout_ height= "Wrap_content"
android:text= "@string/fragment_intro"
/>
<framelayout
android: Id= "@+id/frag_example"
android:layout_width= "match_parent" android:layout_height= "match_parent"
/>
</LinearLayout>
Step Fourth: In the activity, populate the fragment into the framelayout.
public class Fragmentlayouttest extends activity {
@Override
protected void onCreate (Bundle savedinstancestate ) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Get Fragmentmanager
Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Get fragmenttransaction
fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction ();
Get examplefragment
examplefragment fragment = new Examplefragment ();
Add the fragment to the container frag_example
fragmenttransaction.add (r.id.frag_example, fragment);
Fragmenttransaction.commit ();
}
Preferencefragment Use instructions
1. Create a configuration file
new Res/xml/preferences.xml, which reads as follows:
<preferencescreen xmlns:android= "Http://schemas.android.com/apk/res/android" > <preferencecategory androi
D:title= "Preferencecategory a" > <!--(a) Android:key is Preferece's ID (s) android:title is a big title for Preferece
(Android:summary) is the preferece--> <checkboxpreference android:key= "Checkbox_preference" Android:title= "Title_checkbox_preference" android:summary= "summary_checkbox_preference"/> </PreferenceC ategory> <preferencecategory android:title= "Preferencecategory B" > <!--android:dialogtitle is
The title Android:defaultvalue the dialog box is the default value--> <edittextpreference android:key= "Edittext_preference" Android:title= "title_edittext_preference" android:summary= "null" android:dialogtitle= "dialog_title_edittext_p Reference "android:defaultvalue=" null/> <!--android:entries is a description of the items in the list android:entryvalues is the list The value of the items in-->; Listpreference android:key= "list_preference" android:dialogtitle= Choose font "android:entries=" @arra Y/pref_font_types "android:entryvalues=" @array/pref_font_types_values "android:summary=" sans "Android : title= "Font" android:defaultvalue= "sans"/> </PreferenceCategory> <preferencecategory Androi d:title= Preferencecategory C > <switchpreference android:key= switch_preferece android:title= Le_switch_preferece "android:defaultvalue=" true "/> <seekbarpreference android:key=" Seekbar_prefere nCE "android:title=" title_seekbar_preference "android:max=" android:defaultvalue= ""/> </
Preferencecategory> </PreferenceScreen>
Description: Many of the preferencefragment components, including Checkboxpreference, Edittextpreference, Listpreference, Switchpreference, Seekbarpreference, Volumepreference and so on. The properties of these builds are defined as follows. The
(Android:key) is the ID of the preferece, which is the unique identifier of the preferece. The
(android:title) is a preferece headline. The
(android:summary) is a preferece of a small caption. The
(android:dialogtitle) is the title of the dialog box. The
(android:defaultvalue) is the default value. The
(android:entries) is a description of the items in the list. The
(android:entryvalues) is the value of the items in the list.
Note: Switchpreference is supported by API (ANDROID4.0). Therefore, to use switchpreference, you must define the minimum version of APK support in manifest.
<uses-sdk android:minsdkversion=/>
2 custom preferencefragment
public class Prefsfragment extends Preferencefragment implements Sharedpreferences.onsharedpreferencechangelistener,
Preference.onpreferenceclicklistener {private static final String TAG = "# #PrefsFragment # #";
private static final String check_preference = "Checkbox_preference";
private static final String edittext_preference = "Edittext_preference";
private static final String list_preference = "List_preference";
private static final String switch_preference = "Switch_preferece";
private static final String seekbar_preference = "Seekbar_preference";
Private preference Medittext;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Load the preferences from XML resource Addpreferencesfromresource (r.xml.preferences);
Medittext = (preference) findpreference (edittext_preference);
Medittext.setonpreferenceclicklistener (this); @Override public void onsharedpreferencechanged (SharedpreFerences sharedpreferences, String key) {//Set summary to is the user-description for the selected value Prefere
nce connectionpref = findpreference (key);
if (Key.equals (Check_preference)) {Boolean checked = Sharedpreferences.getboolean (key, false);
LOG.D (TAG, "checkbox:checked=" +checked);
else if (key.equals (edittext_preference)) {String value = sharedpreferences.getstring (Key, "");
Connectionpref.setsummary (value);
LOG.D (TAG, "edittext:value=" +value);
else if (key.equals (list_preference)) {String value = sharedpreferences.getstring (Key, "");
Connectionpref.setsummary (value);
LOG.D (TAG, "list:value=" +value);
else if (key.equals (Switch_preference)) {Boolean checked = Sharedpreferences.getboolean (key, false);
LOG.D (TAG, "switch:checked=" +checked);
else if (key.equals (seekbar_preference)) {int value = Sharedpreferences.getint (key, 0);
LOG.D (TAG, "seekbar:value=" +value); @Override public boolean Onpreferenceclick (preference preference) {Sharedpreferences sharedpreference
s = preference.getsharedpreferences ();
String value = sharedpreferences.getstring (Preference.getkey (), "");
LOG.D (TAG, "onpreferenceclick:value=" +value);
return true;
@Override public void Onresume () {super.onresume ();
Getpreferencemanager (). Getsharedpreferences (). Registeronsharedpreferencechangelistener (this); @Override public void OnPause () {Getpreferencemanager (). Getsharedpreferences (). Unregisteronsharedpreferencecha
Ngelistener (this);
Super.onpause ();
}
}
Description: Each item in the Preferencefragment is a Sharedpreferences object that is stored as a sharedpreferences in the APK's private data area. There are a number of ways to listen for members in a preferencefragment, and two common types are:
(01) Monitoring data changes: By implementing the Sharedpreferences.onsharedpreferencechangelistener interface, to monitor the data changes in each item in the Preferencefragment.
(02) Monitoring Click event: By implementing Preference.onpreferenceclicklistener interface, to monitor the preferencefragment of each item in the Click action.
3. Use of Preferencefragment
a preferencefragment has been defined earlier. Next, you can instantiate its object and display it in the activity.
public class Fragmenttest extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Getfragmentmanager (). BeginTransaction (). Replace (Android. R.id.content,
new Prefsfragment ()). commit ();
}