Android fragment instance

Source: Internet
Author: User

Fragment is a new concept of Android honeycomb 3.0. It gives a detailed introduction to fragment in the introduction of Android -- fragment, use of Android fragment, and introduction of Android fragmentmanage fragmenttransaction. This section mainly describes the use of fragment through an instance.

First look at the instance:

The left side is a list, and the right side is the details of the list item.

Take a look at the layout file (layout ):

<?xml version=“1.0″ encoding=“utf-8″?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”    android:orientation=“horizontal” android:layout_width=“match_parent”    android:layout_height=“match_parent”>    <fragment        class=“com.fragment.main.TitlesFragment”        android:id=“@+id/titles” android:layout_weight=“1″        android:layout_width=“0px” android:layout_height=“match_parent” />    <FrameLayout android:id=“@+id/details” android:layout_weight=“1″        android:layout_width=“0px” android:layout_height=“match_parent”        android:background=“?android:attr/detailsElementBackground” /></LinearLayout>

The fragment label and framelayout label are used in the layout file. Android fragment introduces the method of embedding fragment in 2, which is used in this instance. The fragment tag is displayed in the layout file. This is a usage method, the framelayout label will be the second type of view to load fragment.

Let's take a look at the program implementation (COM. Fragment. Main. titlesfragment ):

Public class titlesfragment extends listfragment {int mcurcheckposition = 0; int mshowncheckposition =-1; @ override public void onactivitycreated (bundle savedinstancestate) {super. onactivitycreated (savedinstancestate); setlistadapter (New arrayadapter <string> (getactivity (), android. r. layout. simple_list_item_activated_1, Shakespeare. titles); // use a static array to fill the list if (savedinstancestate! = NULL) {mcurcheckposition = savedinstancestate. getint ("curchoice", 0); mshowncheckposition = savedinstancestate. getint ("shownchoice",-1);} getlistview (). setchoicemode (listview. choice_mode_single); showdetails (mcurcheckposition) ;}@ override public void onsaveinstancestate (bundle outstate) {super. onsaveinstancestate (outstate); outstate. putint ("curchoice", mcurcheckposition); outstate. putint ("Sh Ownchoice ", mshowncheckposition) ;}@ override public void onlistitemclick (listview L, view V, int position, long ID) {showdetails (position );} /*** display listview item details */void showdetails (INT index) {mcurcheckposition = index; getlistview (). setitemchecked (index, true); If (mshowncheckposition! = Mcurcheckposition) {detailsfragment df = detailsfragment. newinstance (INDEX); fragmenttransaction Ft = getfragmentmanager (). begintransaction (); ft. replace (R. id. details, DF); ft. settransition (fragmenttransaction. transit_fragment_fade); ft. commit (); mshowncheckposition = index ;}}}

Titlesfragment

Titlesfragment inherits from the listfragment subclass of fragment. It uses a static array to fill the list and overwrites the onlistitemclick method. The showdetails method shows the details of the listview item.

Detailsfragment df = detailsfragment. newinstance (INDEX); // get the details of fragment instance fragmenttransaction Ft = getfragmentmanager (). begintransaction (); // get the fragmenttransaction instance ft. replace (R. id. details, DF); // use the detailsfragment instance ft. settransition (fragmenttransaction. transit_fragment_fade); ft. commit (); // submit

The second method for loading fragment described in Android fragment is used here. Take a look at detailsfragment:

public class DetailsFragment extends Fragment {     /**     * Create a new instance of DetailsFragment, initialized to     * show the text at ’index’.     */    public static DetailsFragment newInstance(int index) {        DetailsFragment f = new DetailsFragment();        // Supply index input as an argument.                Bundle args = new Bundle();        args.putInt(“index”, index);        f.setArguments(args);        return f;    }     @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        if (container == null) {                        return null;        }        ScrollView scroller = new ScrollView(getActivity());        TextView text = new TextView(getActivity());         int padding = (int) TypedValue.applyDimension(                TypedValue.COMPLEX_UNIT_DIP, 4, getActivity().getResources()                        .getDisplayMetrics());        text.setPadding(padding, padding, padding, padding);        scroller.addView(text);        text.setText(Shakespeare.DIALOGUE[getArguments().getInt("index", 0)]);        return scroller;    }}

In detailsfragment, The newinstance (INT index) method is used to generate a detailsfragment instance and accept Integer Parameters. The oncreateview method is used to create a view.

This example is basically completed, mainly introduced in 3.0 after the use of methods, in fact, fragment after sdk1.6 can be used, in 1.6 use needs to use android-support-v4.jar package implementation. Android-support-v4.jar in: SDK root directory \ extras \ Android \ Compatibility \ V4 can be found, if you want to know how the implementation of fragment on sdk1.6, please refer to fragment in Android sdk1.6 implementation.

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.