Fragment基本描述(二)

 

上一篇我們講解了基本的fragmen使用,這一章節我們講解如何動態加載fragment。下一節我們講解的是fragment的生命週期

 

本示例通過一個fragment啓動另一個fragment。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button butotn = findViewById(R.id.butotn);
        butotn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new FragmenRight());
            }
        });
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fl, fragment);
        fragmentTransaction.commit();
    }
}

對應的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="horizontal"
    tools:context="com.example.administrator.myfragment2.MainActivity">

    <fragment
        android:id="@+id/lift"
        android:name="com.example.administrator.myfragment2.FragmentLift"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"></FrameLayout>

</LinearLayout>

FragmentLift:

public class FragmentLift extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lift, container, false);

        return view;
    }


}

對應的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <Button
        android:id="@+id/butotn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="左側"
        android:gravity="center_horizontal"
        android:textColor="#ffffff" />


</android.support.constraint.ConstraintLayout>

FragmenRight:

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

對應的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    <TextView
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="右側"
        android:textColor="#000000"/>

</android.support.constraint.ConstraintLayout>

addToBackStack(null)的使用

通過調用 addToBackStack(null),可以將替換事務保存到返回棧,以便用戶能夠通過按返回按鈕撤消事務並回退到上一片段。

在MainActivity中:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button butotn = findViewById(R.id.butotn);
        Button button = findViewById(R.id.button);
        butotn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new FragmenRight());
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment2(new FragmenRight());
            }
        });
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fl, fragment);
        fragmentTransaction.commit();
    }

    private void replaceFragment2(Fragment fragment) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fl, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}

對應的xml:

同上

FragmentLift:

同上

FragmentLift對應的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <Button
        android:id="@+id/butotn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="左側"
        android:gravity="center_horizontal"
        android:textColor="#ffffff" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳轉"/>


</android.support.constraint.ConstraintLayout>

轉發表明出處https://blog.csdn.net/qq_35698774/article/details/106426102

點擊下載源碼

感謝:https://developer.android.google.cn/guide/components/fragments

android互助羣:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章