Android to create and add basic --Fragment

Fragment of a new process

Package com.example.myactivityiiii; 

Import android.os.Bundle;
 Import android.view.LayoutInflater;
 Import android.view.View;
 Import android.view.ViewGroup;
 Import androidx.annotation.NonNull;
 Import androidx.annotation.Nullable;
 Import androidx.fragment.app.Fragment; 

/ * 
* a new process Fragment 
* 1. a custom class, based Fragment 
* class layout configuration file 2. 
* 3. onCreateView override method is loaded class layout file, returns a View 
* * / 
public  class ListFragment the extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(
                R.layout.fragment_list,container,false
        );
        return view;
    }
}
package com.example.myactivityiiii;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.zip.Inflater;

public class DetailFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(
                R.layout.fragment_detail,container,false
        );
        return view;
    }
}

Add Fragment in an Activity

1. Direct layout Add: Add the following code in the Main layout file

<fragment
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:name="com.example.myactivityiiii.ListFragment"
    android:id="@+id/list"
    android:layout_weight="1"/>

<fragment
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:name="com.example.myactivityiiii.DetailFragment"
    android:id="@+id/detail"
    android:layout_weight="2"/>

2. Add Fragment in the Activity runs, the main change is the Main code

Add a layout container Main in the layout, the new Fragment thrown into the container like this layout

Main的layout

<?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="horizontal"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main"
        >
    </TextView>

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    </FrameLayout>

</LinearLayout>

Main of java files

package com.example.myactivityiiii;

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

import android.os.Bundle;

/*
* Activity运行时添加Fragment
* */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DetailFragment detailFragment = newDetailFragment ();
         // Get the transaction instance Fragment 
        FragmentTransaction. Ft = getSupportFragmentManager () beginTransaction ();.
         // Specify a container to add detailFragment 
        ft.add (R.id.frame, detailFragment);
         // uncommitted transactions 
        ft.commit (); 

    } 
}

 

Guess you like

Origin www.cnblogs.com/zsben991126/p/12236182.html