《第一行代码Android》学习总结第四章 Fragment的简单介绍

        碎片(Fragment)是一种可以嵌入在活动当中的UI片段,他能让程序更加合理与充分的利用屏幕空间。

一、Fragment的简单使用

1、新建左侧Fragment布局left_fragment.xml与右侧Fragment布局right_fragment.xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00">
    <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>

2、新建LeftFragment类与RightFragment类,分别继承自Fragment类,并实现onCreatView()方法。

public class LeftFragment extends Fragment {
    @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//通过LayoutInflater的inflate()方法动态加载left_fragment.xml
        View view = inflater.inflate(R.layout.left_fragment,container,false);
        return view;
    }
}

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

    }
}

其中,android中存在两个不同包的Fragment:

android.app. Fragment:系统内置,存在版本不兼容问题。

android.support.v4.app. Fragment:support-v4库中Fragment,可以让Fragment在所有Android系统版本中保持功能一致性,所以推荐使用。

3、修改activity_main.xml中代码,通过android:name属性显式指明要添加的碎片类名。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >
 <fragment
     android:id="@+id/left_fragment"
     android:name="com.launcher.fragmenttest.LeftFragment"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"
     />
    <fragment
     android:id="@+id/right_fragment"
     android:name="com.launcher.fragmenttest.RightFragment"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"/>
</LinearLayout>

二、动态添加Fragment

        动态添加Fragment与在布局文件中添加碎片的方法不同,它是将Fragment在程序运行时动态的添加到Activity。

1、新建布局文件another_right_fragment.xml文件,并加入一个TextView。

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

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

2、新建AnotherRightFragment.java 作为另一个右侧的Fragment,重写onCreateView()方法,并加载布局another_right_fragment.xml。

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

3、修改activity_main.xml,添加FrameLayout帧布局,将Fragment动态添加到Activity中。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >
 <fragment
     android:id="@+id/left_fragment"
     android:name="com.launcher.fragmenttest.LeftFragment"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"
     />
//帧布局,Android中最简单的布局,所有控件默认摆放在左上角。
    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
       android:layout_weight="1">
    </FrameLayout>
</LinearLayout>

4、修改MainActivity,实现repalceFragment()方法,完成动态添加Fragment功能。

public class MainActivity extends AppCompatActivity 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);
//动态添加RightFragment
        replaceFragment(new RightFragment());
    }
//动态添加Fragment具体实现
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction transaction = 
fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout,fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
//动态添加AnotherRightFragment
                replaceFragment(new AnotherRightFragment());
                break;
            default:
                break;
        }
    }
}

动态添加碎片的步骤:

1、创建待添加Fragment实例。

2、通过getSupportFragmentManager()方法获取FragmentManager实例

3、调用beginTransaction()方法开启事务

4、使用replace()方法向容器添加或替换碎片,第一个参数为需要传入容器的ID,第二个参数为待添加的Fragment实例。

5、调用commit()方法提交。

三、使Fragment模拟类似Activity返回栈的效果

        FragmentTransaction提供了一个addToBackStack()方法,可以用于将事务添加到返回栈中,其中一个参数接收一个名字用于描述返回栈的状态,一般传入null即可。

四、Fragment与Activity间的通信

1、Activity中调用Fragment方法

        FragmentManager提供了一个findFragmentById()方法,用于从布局文件中获取Fragment实例,再调用碎片中的方法。

RightFragment rightFragment = 
(RightFragment)getSupportFragmentManager().findFragmentById(R.id.right_fragment);

2、Fragment调用Activity方法

        在Fragment中通过调用getActivity()方法得到当前Fragment相关的Activity实例。

MainActivity activity = (MainActivity)getActivity();

        当在Fragment中需要Context对象时,也可以使用getActivity()方法,因为Activity本身就是一个Context。

3、Fragment与Fragment间的通信

        首先,在一个碎片中可以得到与其相关联的活动,再通过这个活动去获取另一个碎片实例。

猜你喜欢

转载自blog.csdn.net/LBW9368/article/details/84029803