Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Fragment components

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Fragment components". In daily operation, I believe many people have doubts about how to use Fragment components. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Fragment components". Next, please follow the editor to study!

I. the relationship between Fragment and Activity

To understand dynamic management Fragment, we must first understand the relationship between Fragment and Activity.

Fragment can be understood as dividing Activity into several fragments. Fragment is a part of Activity. The life cycle is dependent on Activity and cannot exist alone. It is not difficult to understand that all Fragment management (find, add, delete, replace) should be carried out in the Activity that Fragment depends on, that is, Activity is the place for Fragment interaction, do not try to manage another Fragment in one Fragment although it can achieve the required functions but does not conform to the specification. To complete the specific operation on Fragment, you need to use FragmentManager components.

Second, use FragmentManager components

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

/ 1. Create FragmentManager component FragmentManager fm=super.getSupportFragmentManager (); / 2, query method, get a Fragment fm.findFragmentById (R.id.fooler) fm.findFragmentByTag ("tagName") / / 3 in Activity, dynamically add Fragment fm.beginTransaction (). Add (R.id.contenttheory contentFragment, "content"). Commit (); / 4, replace Fragment fm.beginTransaction (). Replace (R.id.OldFragment, newFragment). Commit () / / 5. Delete Fragment fm.beginTransaction () .remove (R.id.myFragment) .commit (); / / 6. Pass data to Fragment f.setArguments (Bundle) method

The above management Fragment code should be organized in the Activity to which Fragment belongs. Let's look at the implementation through a case study.

Third, the realization case

First of all, take a look at this application case (as shown in the figure). When you click the bottom * * area and the second area, you can switch the central area.

Interface analysis: there are three areas: the head region TitleFragment, the central area is a FrameLayout layout that dynamically loads ContentFragment through code, and the bottom area FloorFragment

1. The layout of Activity is as follows

RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width=" match_parent "android:layout_height=" match_parent "> copy code 2, FloorFragment code copy 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 view=inflater.inflate (R.layout.floor_fragment, container,false); LinearLayout home= (LinearLayout) view.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 Activity to reduce the coupling between Activity and Fragment public interface OnContentFragmentListener {void setContentFragment (String param);} private OnContentFragmentListener onContentFragmentListener; public void setOnContentFragmentListener (OnContentFragmentListener onContentFragmentListener) {this.onContentFragmentListener = onContentFragmentListener } @ Override public void onClick (View view) {/ / TODO Auto-generated method stub if {switch (view.getId ()) {case R.id.home: / / callback mechanism to call the method onContentFragmentListener.setContentFragment ("main panel Fragment") of the implementation class; 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 (titlebroken 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.getSupportFragmentManager (). FindFragmentById (R.id.floor); floor.setOnContentFragmentListener (this); / / register to listen, passing in the implementation class object contentFragment=new ContentFragment (); FragmentManager fm=super.getSupportFragmentManager () Fm.beginTransaction (). Add (R.id.contentfront 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.getSupportFragmentManager (). BeginTransaction (). Replace (R.id.content, contentFragment). Commit ();}} this ends the study of "how to use Fragment components", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report