Método de creación de fragmentos de aprendizaje de Android

Hay 2 formas comunes de crear fragmentos

Agregar fragmento en XML

No es necesario moverse en la clase de actividad, en el xml de actividad

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.gaby.test5.CommunicationActivity"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/check_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="是否勾选"/>

    <fragment
        android:id="@+id/communication_fragment"
        android:name="com.gaby.test5.CommunicationFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

Clase CommunicationFragment

public class CommunicationFragment extends Fragment {
    
    


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public CommunicationFragment() {
    
    
    }

  
    public static CommunicationFragment newInstance(String param1, String param2) {
    
    
        CommunicationFragment fragment = new CommunicationFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
    
        //将Fragment绑定的布局文件填充进来
        return inflater.inflate(R.layout.fragment_communication, container, false);;
    }
}

fragment_communication.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gaby.test5.CommunicationFragment"
    android:orientation="horizontal"
    android:background="#000fff">
</LinearLayout>
Agregar fragmento en código Java

Elimine la etiqueta Fragment del archivo xml de diseño donde se encuentra la actividad y agregue un marcador de posición FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.gaby.test5.CommunicationActivity"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/check_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="是否勾选"/>

    <FrameLayout
        android:id="@+id/communication_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

Los siguientes códigos clave en la actividad

public class CommunicationActivity extends AppCompatActivity {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_communication);
        
        //创建Fragment
        CommunicationFragment communicationFragment = new CommunicationFragment();
        //java代码添加Fragment   写法1 
        FragmentManager fm = getSupportFragmentManager();
        //声明事务
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.add(R.id.communication_fragment, communicationFragment)
        fragmentTransaction.commit();
        
        //java代码动态添加 链式写法 写法2
       // getSupportFragmentManager().beginTransaction().add(R.id.communication_fragment, communicationFragment).commit();
        
    }
}

Supongo que te gusta

Origin blog.csdn.net/javaboyweng/article/details/114174574
Recomendado
Clasificación