Android第四章 (碎片)

4.1 碎片的概念(Fragment)
碎片:是一种可以嵌入在活动当中的UI片段,他能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛。
碎片可以被认为是迷你型的活动,在一个活动中可以引用多个碎片,这样就可以充分利用活动界面空间
4.2 碎片的使用方式
4.2.1 碎片的简单用法
1、新建一个左侧碎片布局left_fragment.xml和右侧碎片布局right_fragment,并实现其代码

<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:background="#00ff00"
    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 right fragment"
        />
</LinearLayout>

2、在主包下创建一个LeftFragment类和RightFragment类,均继承Fragment类,重写onCreateView()方法,这样就创造了两个碎片

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

3、在主活动布局activity_main.xml中实现代码

    <fragment
        android:id="@+id/left_fragment"
        android:name="com,example.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight = "1"/>
    <fragment
        android:id="@+id/right_fragment"
        android:name="com,example.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight = "1"/>

4.2.2 动态添加碎片
在上述操作的基础上
1、新建一个another_right_fragmentxml布局文件,实现其代码

<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 another right fragment"
        />
</LinearLayout>

2、新建一个AnotherRightFragment类作为另一个右侧碎片,继承Fragment,实现代码

public class AnotherRightFragment extends Fragment {
    
    
    @Nullable
    @Override
    public View onCreateView(@NonNull 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中,实现代码

    <fragment
        android:id="@+id/left_fragment"
        android:name="com,example.fragmenttest.LeftFragment"
        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">
     </FrameLayout>

4、在主活动中,动态添加碎片,

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);
        //1、在onCreate()中动态创建待添加对象的实例
        replaceFragment(new RightFragment());//换前碎片,直接在屏幕上显示
    }
    @Override
    public void onClick(View v) {
    
    
        switch (v.getId()){
    
    
            case R.id.botton:
                replaceFragment(new AnotherRightFragment());//换前碎片,点击按钮后显示
                break;
                default:
                    break;
        }
    }
    private void replaceFragment(Fragment fragment){
    
    
    	//2、构造placeFragment()方法,获取FragmentManager,在活动中调用getSupportFragmentManager()方法得到
        FragmentManager fragmentManager = getSupportFragmentManager();
        //3、开启一个事务,调用beginTransaction();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //4、向容器内添加或替换碎片,用replace()实现
        fragmentTransaction.replace(R.id.right_layout,fragment);
        //5、调用commit()方法提交事务
        fragmentTransaction.commit();
    }
}
	1)在onCreate中创建动态添加对象的实例
	2)构造placeFragment()方法,获取FragmentManager,在活动中调用getSupportFragmentManager()方法得到
	3)开启一个事务,调用beginTransaction();
	4)向容器内添加或替换碎片,用replace()实现
	5)调用commit()方法提交事务

4.2.3 在碎片中模拟返回栈
如果我们在碎片中摁下Back键,则程序直接退出;若想实现摁下back键回到碎片前一个碎片,即实现返回栈功能,则需要如下操作:
修改MainActivity中的代码:

 private void replaceFragment(Fragment fragment){
    
    
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.right_layout,fragment);
        fragmentTransaction.addToBackStack(null);//该方法可以接受一个名字用于描述返回栈的状态,此时摁下back键,程序会回到前一个碎片界面
        fragmentTransaction.commit();
    }

4.3 碎片的声明周期
4.3.1 碎片的状态和回调
一、状态
1、运行状态 :当一个碎片可见时,且所关联的活动正处于运行状态时,该碎片也处于运行状态
2、暂停状态:当一个活动进入暂停状态,与他相关联的可见碎片就会进入暂停状态
3、停止状态:当一个活动进入停止状态,与它相关联的碎片就会进入停止状态
4、销毁状态:活动被销毁时,碎片也会跟着被销毁
二、回调方法
1、onAttach():当碎片和活动建立关联时调用
2、onCreateView():为碎片创建视图时调用
3、onActivityCreated():确保与碎片相关联的活动一定已经创建完毕时调用
4、onDestroyView():当与碎片关联的视图被移除时调用
5、onDetach():当碎片与活动解除关联时调用
碎片完整的生命周期示意图:
在这里插入图片描述
4.4 动态加载布局的技巧
4.4.1 使用限定符
限定符:用于判断程序应该使用单页模式还是双页模式
如何实现?
1、修改activity_main.xml中的代码:

    <fragment
        android:id="@+id/left_fragment"
        android:name="com,example.fragmenttest.LeftFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

2、在res目录下新建layout_large文件夹,在这个文件夹下新建一个布局,叫做activity_main.xml,代码如下:
在这里插入图片描述
我们通过比较,可以发现上面的布局文件只包含了一个碎片,即单页模式;而下面的布局文件包含了两个碎片,即双页模式。其中layout-large文件夹下的large是个限定符,那些屏幕被认为时large的设备就会自动加载layout-large文件夹下的布局,反之亦然。
在这里插入图片描述
4.4.2 使用最小宽度限定符
最小宽度限定符允许我们对屏幕的宽度指定一个最小值(以dp为基本单位),然后以这个最小值为临界点,屏幕宽度大于这个值的设备就加载一个布局,反之就加载小于这个宽度的布局,例如:
在这里插入图片描述
由此可看出,当屏幕宽度>=600dp的设备上时,会加载上面的布局;反之则加载原来的布局
4.5 小结
1、碎片的概念——是一种可以嵌入在活动当中的UI片段,他能让程序更加合理和充分地利用大屏幕的空间
2、碎片与活动的关系——碎片可以被认为是迷你型的活动,在一个活动中可以引用多个碎片,这样就可以充分利用活动界面空间
3、碎片的简单用法:编写碎片布局文件—>创建相应的类继承—>在指定活动布局文件下引用碎片
4、动态添加碎片:
1)新建一个碎片,并实现类的继承;
2)修改主活动的布局文件中的代码;
3)在主活动类中,动态添加碎片:
–(1)在onCreate中创建动态添加对象的实例
–(2)构造placeFragment()方法,获取FragmentManager,在活动中调用getSupportFragmentManager()方法得到
–(3)开启一个事务,调用beginTransaction();
–(4)向容器内添加或替换碎片,用replace()实现
–(5)调用commit()方法提交事务
5、在碎片中模拟返回栈
6、碎片的声明周期,与活动存在一定的联系
7、限定符的使用

猜你喜欢

转载自blog.csdn.net/Cristiano_san/article/details/106272189