In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to dynamically add fragments in Android", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to dynamically add fragments in Android.
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 ();}} at this point, I believe you have a deeper understanding of "how to dynamically add fragments in Android". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.