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 add fragments dynamically by Android

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

Share

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

This article introduces the knowledge of "how to dynamically add fragments to Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Creation of fragments

To use a fragment, you first need to create a fragment, which is easy to create.

1. Create a new fragment layout, fragment.xml

two。 Create a new class Fragment1.java, which inherits from Fragment

Note that there are two different packages in Fragment. It is recommended to use the one in support-v4 for better compatibility. The other Android will crash below 4.2. Various operations can be performed in this fragment, just like manipulating an activity.

Public class Fragment1 extends Fragment {@ Nullable@Overridepublic View onCreateView (LayoutInflater inflater, @ Nullable ViewGroup container, Bundle savedInstanceState) {View view=inflater.inflate (R.layout.fragmentations1); Log.d ("questionMain1", "fragment 1 loading"); return view;}}

Communication between fragments and activities. Although the fragments are embedded in the activity, the relationship between them is not obvious.

1. The method of the fragment is called in the activity. FragmentManagert provides a method similar to finViewById () to get an instance of the fragment from the layout file. If it is dynamically loaded, it is as simple as loading, and you have an instance of the fragment.

two。 Call the active method in the fragment. You can get the active instance bound to the current fragment through the getActivity () method.

Binding of fragments

1. Static binding

Add a fragment tag to the activity layout, which is relatively simple and not detailed. Android:name= "", this label is the class corresponding to the fragment, and be careful to include the full name of the path.

two。 Dynamic binding

This is the power of fragmentation. Adding fragments dynamically while the program is running, and adding fragments dynamically according to the specific situation, can customize the program interface to be more diversified (mostly for adaptive mobile phone and tablet applications).

The following code clicks the button. There are three fragments that dynamically toggle the displayed fragments in an activity through a click event.

Package com.xiaobu.xiaoyan1.question;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.widget.TextView;import com.xiaobu.xiaoyan1.R;import com.xiaobu.xiaoyan1.base.BaseActivity;public class QuestionsMain extends BaseActivity implements TextView.OnClickListener {private TextView fragment1;private TextView fragment2;private TextView fragment3;@Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activity_question_main); initView ();} private void initView () {((TextView) findViewById (R.id.question_text)) .setTextColor (getResources (). GetColor (R.color.colorTextChecked)); fragment1= (TextView) findViewById (R.id.quiz_text_view); fragment2= (TextView) findViewById (R.id.answer_text_view); fragment3= (TextView) findViewById (R.id.chosen_text_view); fragment1.setOnClickListener (this); fragment2.setOnClickListener (this); fragment3.setOnClickListener (this) ChangeFragment (new QuestionsMain1 ()); checkedChange (fragment1);} @ Overridepublic void onClick (View v) {switch (v.getId ()) {case R.id.quiz_text_view:changeFragment (new QuestionsMain1 ()); break;case R.id.answer_text_view:changeFragment (new QuestionsMain2 ()); break;case R.id.chosen_text_view:changeFragment (new QuestionsMain3 ()); break;default:break;}} private void changeFragment (Fragment fragment) {FragmentManager fragmentManager=getSupportFragmentManager (); FragmentTransaction transaction=fragmentManager.beginTransaction () Transaction.replace (R.id.maintainviewfragment transaction.replace); / / the first parameter represents the id of the container, and the second parameter is the fragment instance. Transaction.commit ();}}

This is the end of "how to dynamically add fragments to Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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