Android nested fragment use instance code _android

Source: Internet
Author: User
Tags deprecated static class

Objective

Previous articles have introduced Activitygroup, many people ask the problem of nested use, the same demand in fragment also exist, fortunately in the latest Android support package has supported this feature! Here we skip the introduction of fragment, and note that Tabactivity has been marked as deprecated (deprecated).

Body

First, prepare

For an introduction to the latest Android Compatibility Pack, see official website. You can find the latest version of the Android SDK directory Extras/android/support/v13/android-support-v13.jar, with the Android 4.2 update.

The introduction of nested fragment, refer to the official website.

Second, screenshot

Third, the Code

Fragmentnestactivity.java

Import Android.graphics.Color;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import android.support.v4.app.FragmentActivity;
Import Android.support.v4.app.FragmentStatePagerAdapter;
Import android.support.v4.app.FragmentTransaction;
Import Android.support.v4.view.ViewPager;
Import android.view.Gravity;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;

Import Android.widget.TextView; /** * Nested Fragment use * * @author farmer Uncle * @see http://www.cnblogs.com/over140/archive/2013/01/02/2842227.html * * * * Lic class Fragmentnestactivity extends Fragmentactivity implements Onclicklistener {@Override protected void oncreat
    E (Bundle arg0) {super.oncreate (arg0);

    Setcontentview (r.layout.nested_fragments);
    Findviewbyid (R.id.btnmodule1). Setonclicklistener (this);
    Findviewbyid (R.id.btnmodule2). Setonclicklistener (this); Findviewbyid (r.id.btnmodule3). Setonclicklistener(this);
  Findviewbyid (R.id.btnmodule1). PerformClick (); @Override public void OnClick (View v) {switch (V.getid ()) {case R.id.btnmodule1:addfragmenttostack
      (fragmentparent.newinstance (0));
    Break
      Case R.id.btnmodule2:addfragmenttostack (fragmentparent.newinstance (1));
    Break
      Case R.id.btnmodule3:addfragmenttostack (Fragmentparent.newinstance (2));
    Break } private void Addfragmenttostack (Fragment Fragment) {fragmenttransaction ft = Getsupportfragmentmanager (). Beg
    Intransaction (); Ft.setcustomanimations (Android. R.anim.slide_in_left, Android.
    R.anim.slide_in_left);
    Ft.replace (R.id.fragment_container, fragment);
  Ft.commit (); }/** nested Fragment */public final static class Fragmentparent extends Fragment {public static final Fragmentpare
      NT newinstance (int position) {Fragmentparent F = new fragmentparent ();
      Bundle args = new Bundle (2); Args.putint ("position", Position);
      F.setarguments (args);
    return F;
      @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
      View Convertview = inflater.inflate (R.layout.viewpager_fragments, container, false);

      Viewpager pager = (Viewpager) Convertview.findviewbyid (R.id.pager);
      Final int parent_position = Getarguments (). GetInt ("position"); Note The code here Pager.setadapter (New Fragmentstatepageradapter (Getchildfragmentmanager ()) {@Override Pub  Lic Fragment getitem (final int position) {return new Fragment () {@Override public View Oncreateview (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {TextView Convertvie
              w = new TextView (getactivity ()); Convertview.setlayoutparams (New Viewgroup.layoutparams (ViewGroup.LayoutParams.FILL_PARENT,
              ViewGroup.LayoutParams.FILL_PARENT)); Convertview.setgravity (Gravity.
              CENTER);
              Convertview.settextsize (30);
              Convertview.settextcolor (Color.Black);
              Convertview.settext ("Page" + position);
            return convertview;
        }
          };
        @Override public int GetCount () {return 3;  @Override public charsequence getpagetitle (int position) {return "Page" + parent_position +
        "-" + position;

      }

      });
    return convertview; }
  }
}

Code Description:

The key here is the support of method Getchildfragmentmanager. This also demonstrates how fragment is used as a nested inner class.

Nested_fragments.xml

<?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" &

  Gt <framelayout android:id= "@+id/fragment_container" android:layout_width= "Fill_parent" android:layout_height=
    "0dip" android:layout_weight= "1.0" android:background= "#F7F5DE" > </FrameLayout> <linearlayout Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_gravity= "Bottom" Andr Oid:background= "@android: Color/black" android:orientation= "horizontal" > <imageview android:id= "@+id/ BtnModule1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_marg" Inbottom= "3DP" android:layout_marginleft= "7DP" android:layout_margintop= "3DP" android:src= "@android:d Raw Able/ic_dialog_dialer "/>

    <imageview android:id= "@+id/btnmodule2" android:layout_width= "Wrap_content" Android:layout_heig ht= "Wrap_content" android:layout_marginbottom= "3DP" android:layout_marginleft= "7DP" Android:layout_margi ntop= "3DP" android:src= "@android:d rawable/ic_dialog_info"/> <imageview android:id= "@+id/btnmodule3 "Android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout_marginbottom=" 3DP "android:layout_marginleft=" 7DP "android:layout_margintop=" 3DP "android:src=" @android:d Rawable/ic_di Alog_alert "/> </LinearLayout> </LinearLayout>

Viewpager_fragments.xml

<?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.support.v4.view.ViewPager
    android:id= "@+id/pager"
    android:layout_width= "Match_parent"
    android:layout_height= "Match_parent" >

    <android.support.v4.view.pagertitlestrip
      android:layout_ Width= "Wrap_content"
      android:layout_height= "wrap_content"
      android:layout_gravity= "top"/>
  </android.support.v4.view.ViewPager>

</LinearLayout>

Code Description:

Attention! Practice found that Viewpager is not a top-level container, otherwise it will be an error.

Iv. description

This is a typical nested fragment example, the outermost use of framelayout to achieve several major modules of the switch, the internal use of Viewpager implementation of the sub-module switching, very practical.

End

Consider support Package, revision 11 update translation, we strongly recommend that you upgrade to the latest Compatibility pack.

Related Article

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.