Android《第一行代码》——第四章 碎片

碎片

碎片是一种可以嵌入在活动中的UI片段,它能够使程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的十分广泛。碎片的作用实质上就是可以在一个屏幕中有多个独立的UI界面。

碎片的使用方法

碎片静态用法

  • 给碎片建立布局。
  • 创建碎片的java类,继承Fragment类。
  • 在活动布局中引入该碎片(用android:name属性来确定是哪个碎片)。

左布局

<?xml version="1.0" encoding="utf-8"?>
<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>

右布局

<?xml version="1.0" encoding="utf-8"?>
<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>

左碎片类

package com.example.fragmenttest;

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

import androidx.fragment.app.Fragment;

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

}

右碎片类

package com.example.fragmenttest;

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

import androidx.fragment.app.Fragment;

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

}

主布局

<?xml version="1.0" encoding="utf-8"?>
<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.example.fragmenttest.LeftFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
         <fragment
        android:id="@+id/right_fragment"
        android:name="com.example.fragmenttest.RightFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

碎片动态用法

  • 给碎片建立布局
  • 创建碎片的java类继承Fragment类
  • 在活动的布局中准备碎片存放的布局。
  • 在活动的代码中先获取碎片的Manager实例,再获得碎片的Transaction实例,再调用replace()方法向准备好的布局中引入碎片,再调用commit()方法即可完成动态引入碎片。

新碎片布局

<?xml version="1.0" encoding="utf-8"?>
<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>

新碎片类

package com.example.fragmenttest;

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

import androidx.fragment.app.Fragment;

public class AnotherRightFragment extends Fragment {

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

主布局

<?xml version="1.0" encoding="utf-8"?>
<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.example.fragmenttest.LeftFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

主活动类

package com.example.fragmenttest;

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

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

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);
        replaceFragment(new RightFragment());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                replaceFragment(new AnotherRightFragment());
                break;
            default:
                break;
        }
    }
    private void replaceFragment(Fragment fragment){
        FragmentManager  fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_fragment,fragment);
        transaction.commit();
    }
}

碎片中模拟返回栈

在replaceFragment中commit()前添加函数。

 FragmentManager manager=getSupportFragmentManager();
 FragmentTransaction transaction=manager.beginTransaction();
 transaction.replace(R.id.right_layout,new RightFragment());
 transaction.addToBackStack(null);//将该碎片加入返回栈
 transaction.commit();

碎片和活动之间的通信

//在活动中获取碎片实例
LeftFragment leftFragment=(LeftFragment) getSupportFragmentManager().findFragmentById(R.id.left_fragment);
//在碎片中获取活动实例
MainActivity activity=(MainActivity) getActivity();

碎片的生命周期

碎片的状态与回调

1.运行状态
当一个碎片是可见的,并且和它相关联的活动正处于运行状态时,该碎片也处于运行状态。

2.暂停状态
当一个活动进入暂停状态时,和它相关联的活动也处于暂停状态。

3.停止状态
当一个活动进入停止状态时,和它相关联的活动也处于停止状态。或者调用FragmentTransaction的remove()或replace()方法将碎片从活动中移除,但在事务提交之前调用了addToBackStack()方法,这时碎片也会进入停止状态。

4.销毁状态
碎片总是依附于活动而存在的,因此当活动销毁时,与它相关联的碎片也会进入销毁状态。或者调用FragmentTransaction的remove()或replace()方法将碎片从活动中移除,但在事务提交之前并没有调用addToBackStack()方法,这时碎片也会进入销毁状态。
onAttach():当碎片于活动建立联系时调用。
onCreateView():为碎片加载布局时调用。
onActivityCreated():确保于碎片相关联的活动一定已经创建完毕时调用。
onDestoryView():当与碎片相关联的视图被移除时调用。
onDetach():当碎片与活动解除联系时调
请添加图片描述

动态加载布局的技巧

使用限定符

只需新建带限定符的layout文件,并重写UI界面即可在不同设备加载UI界面。

屏幕特征限定符描述
大小small 提供给小屏幕设备的资源
normal提供给中等屏幕设备的资源
large 提供给大屏幕设备的资源
xlarge提供给超大屏幕设备的资源
分辨率 ldpi提供给低分辨率设备的资源(120dpi以下)
mdpi提供给中等分辨率设备的资源(120dpi到160dpi
hdpi提供给高分辨率设备的资源(160dpi到240dpi)
xhdpi提供给超高分辨率设备的资源(240dpi到320dpi)
方向land提供给横屏设备的资源
port提供给竖屏设备的资源

使用最小宽度限定符

最小宽度限定符允许我们对屏幕的宽度指定一个最小值(以dp为单位),然后以这个最小值为临界点,屏幕宽度大于这个值的设备就加载一个布局,屏幕宽度小于这个值的设备就加载另一个布局。
res下新建layout-sw600dp文件夹,里面新建activity_main.xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值