Fragment

Fragment

Tags: Android

摘要

Fragment,静态添加Fragment,动态添加Fragment

什么是Fragment

碎片(Fragment)是一种可以嵌入在活动当中的 UI 片段,它能让程序更加合理和充分
地利用大屏幕的空间,因而在平板上应用的非常广泛。虽然碎片对你来说应该是个全新的概
念,但我相信你学习起来应该毫不费力,因为它和活动实在是太像了,同样都能包含布局,
同样都有自己的生命周期。你甚至可以将碎片理解成一个迷你型的活动,虽然这个迷你型的
活动有可能和普通的活动是一样大的

静态添加Fragment

left_fragment.xml

<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>

right_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>

对应的java代码

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;
}
}

main中引用一下

<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="net.javablog.test41_fragment.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/right_fragment"
android:name="net.javablog.test41_fragment.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>

静态添加的效果

提示警告不用理会

动态添加Fragment

新创建一个fragment

another_right_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"
    android:orientation="vertical"
    android:tag="right02">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="This is another right fragment"
        android:textSize="20sp" />

</LinearLayout>

改动main

<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="net.javablog.test41_fragment.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <!--<fragment-->
    <!--android:id="@+id/right_fragment"-->
    <!--android:name="net.javablog.test41_fragment.RightFragment"-->
    <!--android:layout_width="0dp"-->
    <!--android:layout_height="match_parent"-->
    <!--android:layout_weight="1" />-->

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

        <fragment
            android:id="@+id/right_fragment"
            android:name="net.javablog.test41_fragment.RightFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="right" />
    </FrameLayout>


</LinearLayout>

MainActivity动态添加Fragment

package net.javablog.test41_fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);

        //在 Activity中获取Fragment实例
//        RightFragment rightFragment = (RightFragment) getFragmentManager()
//                .findFragmentById(R.id.right_fragment);

    }

    public Fragment currentFragment = new RightFragment();

    //动态替换右侧的fragment
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:


                FragmentManager fragmentManager = getFragmentManager();
                Fragment fragment = new RightFragment();
                //交替替换.
                if (currentFragment.getClass() == fragment.getClass()) {
                    fragment = new AnotherRightFragment();
                } else {
                }

                FragmentTransaction transaction = fragmentManager.
                        beginTransaction();
                transaction.replace(R.id.right_layout, fragment);
                transaction.addToBackStack(null);
                transaction.commit();
                currentFragment = fragment;

                break;
            default:
                break;
        }
    }
}

动态添加Fragment效果