Fragment Detailed-android learning Journey (48)

Source: Internet
Author: User

Fragment's Philosophy of design

The inheritance system of fragment

Most of the development of fragment will inherit the following three methods

Fragment communication with the activity


Fragment interaction with activity information


Management and fragment Affairs of fragment

Develop a resolution-balanced application


Develop fragment books to show the details of the books fragment class machine layout
 PackagePeng.liu.test;ImportAndroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView;/** * Created by LPLP on 2015/6/26. * * Public  class bookdetailfragment extends Fragment {     Public Static FinalString item_id ="item_id"; Bookcontent.book Book;@Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);if(Getarguments (). ContainsKey (item_id))        {book = BookContent.ITEM_MAP.get (Getarguments (). GetInt (item_id)); }    }@Override     PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View Bookview = inflater.inflate (R.layout.cell,container,false);if(Book! =NULL{(TextView) Bookview.findviewbyid (R.id.title)). SetText (Book.title);        ((TextView) Bookview.findviewbyid (R.id.desc)). SetText (BOOK.DESC); }returnBookview; }}
<?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="vertical">        <textview  android:layout _width  = "fill_parent"  android:layout_height< /span>= "wrap_content"  android:id  = "@+id/title"  android:padding  = "16DP"  style  = "android:attr/ Textappearancelarge "/>     <TextViewandroid:layout_width="Fill_parent"android:layout_height= "Wrap_content" android:id="@+id/desc"android:padding= "16dp"style="? Android:attr/textappearancelarge "/>                                        </linearlayout>
List of books the class was closed. He, because inherits the listfragment, therefore does not need the layout file, Setlsitadapetr can
 PackagePeng.liu.test;Importandroid.app.Activity;ImportAndroid.app.ListFragment;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;/** * Created by LPLP on 2015/6/26. * * Public  class booklistfragment extends listfragment {    PrivateCallbacks Mcallbacks; Public  Interface callbacks{         Public void onitemselected(Integer ID); }@Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setlistadapter (NewArrayadapter<bookcontent.book> (Getactivity (), Android. R.layout.simple_list_item_activated_1,android.    R.id.text1,bookcontent.items)); }@Override     Public void Onattach(Activity activity) {Super. Onattach (activity);if(! (ActivityinstanceofCallbacks)) {Throw NewIllegalStateException ("The activity of the booklistfragment must implement the callbacks interface");    } Mcallbacks = (callbacks) activity; }@Override     Public void Ondetach() {Super. Ondetach (); Mcallbacks =NULL; }@Override     Public void Onlistitemclick(ListView L, View V,intPositionLongID) {Super. Onlistitemclick (L, V, position, id);    Mcallbacks.onitemselected (BookContent.ITEMS.get (position). ID); } Public void Setactivateonitemclick(BooleanActivateonitemclick) {Getlistview (). Setchoicemode (Activateonitemclick?    ListView.CHOICE_MODE_SINGLE:ListView.CHOICE_MODE_NONE); }}
Main activity class and its layout
 PackagePeng.liu.test;Importandroid.app.Activity;ImportAndroid.app.ProgressDialog;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.os.AsyncTask;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.RadioButton;ImportAndroid.widget.TextView;ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.net.MalformedURLException;ImportJava.net.URL;ImportJava.net.URLConnection; Public  class mainactivity extends Activity  implements  Booklistfragment. Callbacks {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);    Setcontentview (R.layout.activity_main); }@Override     Public void onitemselected(Integer ID) {Bundle Bundle =NewBundle ();        Bundle.putint (Bookdetailfragment.item_id,id); Bookdetailfragment fragment =NewBookdetailfragment ();        Fragment.setarguments (bundle);    Getfragmentmanager (). BeginTransaction (). replace (r.id.bookcontainer,fragment). commit (); }}
<?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  =;     <fragment  android:layout _width  = "1DP"  android:layout_height  =< Span class= "Hljs-value" > "fill_parent"  android:name  =" peng.liu.test.BookDetailFragment " android:id  = "@+id/bookdetail"  android:layout_weight  =" 1 "/>     <framelayoutandroid:layout_width= "1dp"android:layout_height=" Fill_parent "android:id=" @+id/bookcontainer "android:layout_weight=" 3 "/ >                                </linearlayout>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Fragment Detailed-android learning Journey (48)

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.