Some summaries of Fragment s

 

Catalog

1. Creation of Fragment s

        1,XML

Error-prone point 1: If a fragment is not wrapped outside by a layer out, you must give it an id, otherwise an error will occur, which is not possible like the following

2. Dynamic Creation

Error-prone point one: FragmentTransaction can only be submitted once, so you can put multiple operations together for the last time; otherwise, use getSupportFragmentManager().beginTransaction()

Error-prone point 2: When loading dynamically, activity findviewbyid finds the fragment's control id, but gets empty. If you try to set a click event for this control, you will get an error. You need to set a click event in the onCreateView of the fragment to which the control belongs

2. Deletion of Fragment s

Error-prone: Executing remove is useless if the fragment currently displayed is not the one to be deleted

3. Replacement of Fragment s

Error-prone point 1: As shown in the following figure, the fragment is not empty after replace is measured. If it is placed inside if(fragmentLeft==null), then when switching back, the statements inside if will not be executed, so nothing will be displayed

Error-prone point 2: Whether you replace with add or replace, pressing the return key will exit the APP if you do not add ToBackStack, and if you add, the replace will fall back to the last fragment instead of exiting directly

The following is the test code for add and replace

Replacement by add:

Effect of replacing:

1. Creation of Fragment s

        1,XML

Define a layout for the fragment first, then create a new class to inherit the fragment and load the layout in onCreateView.

* Then included in the activity xml through the <fragment/>tag, most importantly its attribute name

Error-prone point 1: If a fragment is not wrapped outside by a layer out, you must give it an id, otherwise an error will occur, which is not possible like the following

However, with a layer of layout outside, it is also possible to use no id, as follows

2. Dynamic Creation

* Do not operate in XML, just new in activity

Error-prone point one: FragmentTransaction can only be submitted once, so you can put multiple operations together for the last time; otherwise, use getSupportFragmentManager().beginTransaction()

Error-prone point 2: When loading dynamically, activity findviewbyid finds the fragment's control id, but gets empty. If you try to set a click event for this control, you will get an error. You need to set a click event in the onCreateView of the fragment to which the control belongs

2. Deletion of Fragment s

Error-prone: Executing remove is useless if the fragment currently displayed is not the one to be deleted

3. Replacement of Fragment s

There are two ways to do this: add or replace--->hide all fragments-->show all the fragments that need to be displayed

The difference is that when add is not refreshed, replace is refreshed

Error-prone point 1: As shown in the following figure, the fragment is not empty after replace is measured. If it is placed inside if(fragmentLeft==null), then when switching back, the statements inside if will not be executed, so nothing will be displayed

Error-prone point 2: Whether you replace with add or replace, pressing the return key will exit the APP if you do not add ToBackStack, and if you add, the replace will fall back to the last fragment instead of exiting directly

The following is the test code for add and replace

The code is as follows:

left_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I'm on the left"
        android:id="@+id/leftText" />
</LinearLayout>

 middle_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I'm in the middle"
        android:id="@+id/middleText" />
</LinearLayout>

right_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I'm on the right"
        android:id="@+id/rightText" />
</LinearLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/frameLayout"
        android:layout_weight="6">
    </FrameLayout>

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="0dp" >
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="left"
            android:layout_weight="1"
            android:id="@+id/leftBtn" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Middle"
            android:id="@+id/middleBtn" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Right"
            android:id="@+id/rightBtn" />
    </LinearLayout>
</LinearLayout>

All three fragments are similar, as follows:

Finally, MainActivity is as follows: The main thing is to try add and replace, respectively.

package com.example.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    private Button leftBtn;
    private Button midBtn;
    private Button rigBtn;

    private Fragment fragmentLeft;
    private Fragment fragmentMid;
    private Fragment fragmentRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        leftBtn = (Button) findViewById(R.id.leftBtn);
        rigBtn = (Button) findViewById(R.id.rightBtn);
        midBtn = (Button) findViewById(R.id.middleBtn);

        leftBtn.setOnClickListener(this);
        rigBtn.setOnClickListener(this);
        midBtn.setOnClickListener(this);
        initFragmentleft();
//        getSupportFragmentManager().beginTransaction().add(R.id.frameLayout,new LeftFrag()).commit();

    }

    private void add() {
        getSupportFragmentManager().beginTransaction().add(R.id.frameLayout,new MiddleFragment()).commit();
    }

    private void replace() {
        getSupportFragmentManager().beginTransaction().addToBackStack(null).add(R.id.frameLayout,new RightFrag()).commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.leftBtn:
//                add();
                initFragmentleft();
                break;
            case R.id.middleBtn:
//                replace();
                initFragmentMid();
                break;
            case R.id.rightBtn:
                initFragmentRig();
                break;
        }
    }

    private void initFragmentleft() {
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        if (fragmentLeft == null) {
            fragmentLeft = new LeftFrag();
            transaction.add(R.id.frameLayout,fragmentLeft);
//                        transaction.replace(R.id.frameLayout,fragmentLeft); //Error prone, can't replace here

        }
//        transaction.replace(R.id.frameLayout,fragmentLeft);
        hideAllFragment(transaction);
        transaction.show(fragmentLeft);
        transaction.commit();
    }

    private void hideAllFragment(FragmentTransaction transaction) {
        if (fragmentLeft != null) {
            transaction.hide(fragmentLeft);
        }
        if (fragmentMid != null) {
            transaction.hide(fragmentMid);
        }
        if (fragmentRight != null) {
            transaction.hide(fragmentRight);
        }
    }

    private void initFragmentMid() {
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        if (fragmentMid == null) {
            fragmentMid = new MiddleFragment();
            transaction.add(R.id.frameLayout,fragmentMid);
//            transaction.replace(R.id.frameLayout,fragmentMid);
        }
//        transaction.replace(R.id.frameLayout,fragmentMid);

        hideAllFragment(transaction);
        transaction.show(fragmentMid);
        transaction.commit();
    }

    private void initFragmentRig() {
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        if (fragmentRight == null) {
            fragmentRight = new RightFrag();
            transaction.add(R.id.frameLayout,fragmentRight);
//            transaction.replace(R.id.frameLayout,fragmentRight);
        }
//        transaction.replace(R.id.frameLayout,fragmentRight);
        hideAllFragment(transaction);
        transaction.show(fragmentRight);
        transaction.commit();
    }


}

Replacement by add:

 

Effect of replacing:

 

               

               

Tags: Android Fragment

Posted on Mon, 13 Sep 2021 17:57:31 -0400 by may27