In-depth analysis of dynamic Fragment management, in-depth analysis of fragment

Source: Internet
Author: User

In-depth analysis of dynamic Fragment management, in-depth analysis of fragment

1. Relationship between Fragment and Activity

To understand how to dynamically manage Fragment, first understand the relationship between Fragment and Activity.

Fragment can be understood as dividing an Activity into several fragments. Fragment is a part of the Activity, and its lifecycle depends on the Activity. It cannot exist independently. It is difficult to understand that all Fragment Management (search, add, delete, and replace) should be carried out in the Activity on which Fragment depends, that is, the Activity is the place where Fragment interacts, do not try to manage another Fragment in one Fragment. Although the required functions can also be implemented, they do not conform to the specifications. To complete Fragment operations, you must use the FragmentManager component.

Ii. Use the FragmentManager component

The key code for using the FragmentManager component is as follows:

// 1. Create the FragmentManager component FragmentManager fm = super. getsuppfrfragmentmanager (); // 2. query method to obtain a Fragmentfm in the Activity. findFragmentById (R. id. fooler) fm. findFragmentByTag ("tagName") // 3. Add Fragmentfm dynamically. beginTransaction (). add (R. id. content, contentFragment, "content "). commit (); // 4. Replace Fragmentfm. beginTransaction (). replace (R. id. oldFragment, newFragment ). commit (); // 5. Delete Fragmentfm. beginTransaction (). remove (R. id. myFragment ). commit (); // 6. transmit data to Fragment. setArguments (Bundle) method to transmit data

The above Fragment Management Code should be organized in the Activity to which Fragment belongs. The following uses a case to see the specific implementation.

Iii. Implementation case

First, let's take a look at this application case (). When you click the first region and the second region at the bottom, switch the center region.

Interface Analysis: There are three areas: the header area TitleFragment. The center area is a FrameLayout layout. The code dynamically loads ContentFragment, and the bottom area is FloorFragment.

1. The Activity layout is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <fragment android:id="@+id/fgTitle"          android:layout_width="match_parent"    android:layout_height="45dp"           android:name="com.jereh.android.course.fragment.TitleFragment"        />    <FrameLayout android:id="@+id/content"        android:layout_width="match_parent"        android:layout_height="match_parent"         android:layout_below="@id/fgTitle"        />     <fragment android:id="@+id/floor"          android:layout_width="match_parent"        android:layout_height="wrap_content"         android:layout_alignParentBottom="true"        android:orientation="horizontal"           android:name="com.jereh.android.course.fragment.FloorFragment"        />        </RelativeLayout>
2. FloorFragment code
Public class FloorFragment extends Fragment implements OnClickListener {@ Override public void onAttach (Activity activity) {super. onAttach (activity) ;}@ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// TODO Auto-generated method stub View = inflater. inflate (R. layout. floor_fragment, container, false); LinearLayout home = (LinearLayout) v Iew. findViewById (R. id. home); LinearLayout list = (LinearLayout) view. findViewById (R. id. list); home. setOnClickListener (this); list. setOnClickListener (this); return view;} // defines the interface, which is implemented in the Activity to reduce the Coupling Degree between the Activity and Fragment. public interface OnContentFragmentListener {void setContentFragment (String param );} private OnContentFragmentListener onContentFragmentListener; public void setOnContentFragmentListen Er (OnContentFragmentListener onContentFragmentListener) {this. onContentFragmentListener = onContentFragmentListener;} @ Override public void onClick (View view) {// TODO Auto-generated method stub if (response! = Null) {switch (view. getId () {case R. id. home: // callback mechanism, which calls the onContentFragmentListener method of the implementation class. setContentFragment ("main panel Fragment"); break; case R. id. list: onContentFragmentListener. setContentFragment ("list information"); break ;}}}}
3. ContentFragment code
public class ContentFragment extends Fragment {    private String title;    public void setArguments(Bundle args) {        title=args.getString("title");    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View view=inflater.inflate(R.layout.content_fragment, container,false);        if(title!=null)((TextView)view.findViewById(R.id.tvContent)).setText(title);        return view;    }}
4. DynaFragmentActivity code
Public class DynaFragmentActivity extends FragmentActivity implements FloorFragment. onContentFragmentListener {private ContentFragment contentFragment; private FloorFragment floor; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); super. setContentView (R. layout. dyna_fragment_activity); initFragment ();} private void initFragment () {floor = (FloorFragment) super. getsuppfrfragmentmanager (). findFragmentById (R. id. floor); floor. setOnContentFragmentListener (this); // register the listener and pass in the implementation Class Object contentFragment = new ContentFragment (); FragmentManager fm = super. getsuppfrfragmentmanager (); fm. beginTransaction (). add (R. id. content, contentFragment, "content "). commit ();} public void setFragment (Fragment fragment) {getSupportFragmentManager (). beginTransaction (). replace (R. id. content, fragment ). commit () ;}@ Override public void setContentFragment (String param) {// TODO Auto-generated method stub Bundle bundle = new Bundle (); bundle. putString ("title", param); contentFragment = new ContentFragment (); contentFragment. setArguments (bundle); super. getsuppfrfragmentmanager (). beginTransaction (). replace (R. id. content, contentFragment ). commit ();}}

 

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.