Android learning Fragment
- 2020-12-21 18:09:08
- OfStack
What is Fragment
A fragment (Fragment) is an UI fragment that can be embedded in an activity (activity).
1. Simple use of fragments
Create two layout files:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"
/>
</LinearLayout>
//left_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is right fragment"
/>
</LinearLayout>
//right_fragment.xml
All custom Fragment needs to inherit from the Fragment class:
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false); return view;
}
}
public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, container, false); return view;
}
}
Finally, es20EN_ES21en.xml is defined
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/right_fragment"
android:name="com.example.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
2. Add fragments dynamically
You can add fragments dynamically in your code:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
AnotherRightFragment fragment = new AnotherRightFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.
beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.commit();
break;
default:
break;
} }
The dynamic addition of debris is mainly divided into five steps.
1. Create the fragment instance to be added.
2. Get FragmentManager, which can be obtained by calling getFragmentManager() directly in the activity.
3. Start a transaction by calling beginTransaction().
4. Add shards to the container. Generally, replace() method is used to implement, which requires passing id of the container and shard instance to be added.
5. Commit the transaction and call commit() to complete.
3. Simulate return stack in shards
After adding a fragment by clicking the button, pressing the Back key will cause the program to exit.
public class MainActivity extends Activity implements OnClickListener {
......
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
AnotherRightFragment fragment = new AnotherRightFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.
beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
break;
default:
break; }
}
}
4. Communication between debris and activities
To facilitate communication between fragments and activities,FragmentManager provides a method similar to findViewById() specifically for retrieving instances of fragments from layout files, as shown below:
RightFragment rightFragment = (RightFragment) getFragmentManager()
.findFragmentById(R.id.right_fragment);
Within each fragment, the active instance associated with the current fragment can be obtained by calling the getActivity() method, as shown below:
MainActivity activity = (MainActivity) getActivity();
5. The life cycle of debris
Running state: When a fragment is visible and the activity associated with it is running, the fragment is also running.
Paused: When an activity is paused (because another activity that does not occupy the screen is added to the top of the stack), the visible fragment associated with it is paused.
Stop state: When an activity enters the stop state, the fragment associated with it enters the stop state.
Destruction state: The fragment is always attached to the activity, so when the activity is destroyed, the fragment associated with it will enter the destruction state.
Here are some of the shard's callback methods:
onAttach() is called when a fragment is associated with an activity.Called by onCreateView() when creating a view for fragmentation (loading the layout).
onActivityCreated() ensures that the activity 1 associated with the fragment is called when it has been created.
onDestroyView() is called when the view associated with the fragment is removed.
onDetach() is called when the fragment and activity are unassociated.
The above is all about the fragment Fragment in Android, I hope to help you with your study.