Android dynamically adding fragmented code instances

  • 2021-11-10 10:38:17
  • OfStack

Creation of fragments

To use a fragment, you must first create a fragment, which is very simple.

1. Create a new fragment layout, fragment. 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" This is a fragment 1"/>
</LinearLayout>

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

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


public class Fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_questions1,container,false);
Log.d("questionMain1"," Fragments 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. Invoke fragmented methods in the activity. FragmentManagert provides a method similar to finViewById () to get an instance of the fragment from the layout file. If it is loaded dynamically, it is as simple as loading, and you have an instance of the fragment.

2. Invoke the active method in the fragment. The active instance bound to the current fragment can be obtained through the getActivity () method.

Binding of fragments

1. Static binding

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


<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" This is a fragment 3"/>
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

2. Dynamic binding

This is the powerful part of fragments. When the program is running, it is dynamically added to fragments. According to the specific situation, it can dynamically add fragments, and the program interface can be customized to be more diversified (mostly used for adaptive mobile phone and tablet applications)

The following code to click the button. There are 3 fragments, and the displayed fragments are dynamically switched in one activity by clicking the 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;
@Override
protected 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);
}
@Override
public 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.main_view,fragment);// No. 1 1 Parameters represent the container's id , No. 2 Parameters are fragment instances. 
transaction.commit();
}
}

Related articles: