从0系统学Android--4.1探究碎片

从0系统学Android--4.1探究碎片


本系列文章目录更多精品文章分类

本系列持续更新中.... 初级阶段内容参考《第一行代码》

第四章:手机平板要兼顾--探究碎片

平板电脑和手机最大的区别就在于屏幕的大小,一般手机的屏幕大小会在 3 英寸到 6 英寸之间,而一般平板电脑屏幕大小会在 7 英寸到 10 英寸之间。屏幕大小差距过大会导致同样的界面视觉效果有很大的差异。

为了兼顾手机和平板开发,Android 3.0 引入了碎片的概念,可以让界面在平板上更好的展示。

4.1 碎片是什么

碎片(Fragment)是一种可以嵌入到 Activity 中的 UI 片段,让程序更加合理和充分利用屏幕的空间。它和 Activity 很像,同样都能包含布局,同样有生命周期。

如何利用平板的屏幕空间呢?比如我们要开发一个新闻类的 APP。在手机端可以是这样的。

可以是如果在平板上也这样设计,那么新闻标题列表就会给拉伸的很长,而新闻的标题一般都不会太长,这样设计就会导致页面不合理。

因此,更好的设计方案是将新闻列表和新闻详细内容界面放到两个碎片中,然后在同一 Activity 中引入这两个碎片,这样屏幕空间就充分利用起来了。

4.2 碎片的使用方式

首先我们先创建一个平板的模拟器,准备好后新建一个包用于碎片化的练习。

4.2.1 碎片的简单用法

写一个最简单的碎片示例,在一个 Activity 中添加两个碎片,并让这两个碎片平方 Activity 空间。

  1. 新建一个左侧碎片布局 left_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="center_horizontal"
        android:id="@+id/bt"/>
</LinearLayout>
  1. 新建一个右侧碎片布局right_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000"
    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>
  1. 新建 LeftFragment 类,让他继承 Fragment,Fragment 可能会有两个不同的包,建议使用支持库中的 Fragment,因为它可以让 Fragment 在所有的 Android 系统版本中保持功能一致性。比如在 Fragment 中嵌套 Fragment ,如果你使用的是系统内置的 Fragment 则在 Android 4.2 系统之前的设备上运行程序会崩溃。
public class LeftFragment extends Fragment {
    @Nullable
    @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;

    }
}
  1. 同样的方法再创建一个 RightFragment
public class RightFragment extends Fragment {
    @Nullable
    @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;

    }
}
  1. 修改 fragmentbaseuse_activity 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.firstcode.fourth_chapter.LeftFragment"
        android:id="@+id/fg_left"/>
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.firstcode.fourth_chapter.RightFragment"
        android:id="@+id/fg_right"/>

</LinearLayout>

这里使用了 <fragment> 标签在布局中添加碎片,然后在标签中通过 android:name 属性来指明要添加的碎片的类名,注意一定要把包名加上。

运行结果:

4.2.2 动态添加碎片

在上一节中我们学习了如何在布局中添加碎片,下面我们来学习如何用代码动态的添加碎片。

新建一个 another_right_fragment

<?xml version="1.0" encoding="utf-8"?>
<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">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="another fragment"/>
</LinearLayout>

里面代码基本相同,只是更该了一下背景颜色,用来区分。

再新建一个 Fragement

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;

    }
}

修改主页面的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.firstcode.fourth_chapter.LeftFragment"
        android:id="@+id/fg_left"/>
   <!-- <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.firstcode.fourth_chapter.RightFragment"
        android:id="@+id/fg_right"/>-->
    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/framelayout"/>

</LinearLayout>

<fragment>替换成了 FrameLayout ,FragmentLayout 是 Android 中最简单的一种布局,所有的控件默认会摆放在布局的左上角。这里仅需要放入一个碎片,不需要任何定位,因此非常适合使用 FrameLayout

下面在代码中向 FrameLayout 中添加内容,从而实现动态添加碎片的功能。

public class FragmentBaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragmentbase);
        Button button = findViewById(R.id.bt);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new AnotherRightFragment());
            }
        });
        replaceFragment(new RightFragment());

    }

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

首先给左侧碎片中的按钮注册了点击事件,然后调用了 replaceFragment() 方法动态的添加了 RightFragment 这个碎片。当点击左侧按钮的时候,就会触发 replaceFragment() 这个方法。

动态添加碎片主要分为 5 步:

  1. 创建待添加的碎片
  2. 获取 FrgmentManagerFragmentManager是一个抽象类,在 Activity 中通过 getSupportFragmentManager() 方法来获取。
  3. 开启一个事务,通过调用 beginTransaction() 方法开启
  4. 向容器中添加或者替换掉已经添加的碎片,一般使用 replace() 就可以了。
  5. 提交事务,调用 commit() 方法来完成。

这样就完成了。

4.2.3 在碎片中模拟返回栈

在上一小节中已经学习了如何动态添加碎片,不过当我们按下 Back 键程序就直接退出了,如何实现类似于返回栈的效果,当按下 back 键的时候返回到上一个碎片呢?

FragmentTransaction 中提供了一个 addToBackStack() 方法,可以用于将一个事务添加到返回栈中。

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.replace(R.id.framelayout,fragment);
        // 可以接受一个名字用于描述返回栈的状态,一般传入 null 即可 会将这个 Fragment 添加到栈中
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

这样再运行程序,你会发现按下 Back 后程序没有立马退出,而是先退出了 Fragment,等 Fragment 界面也消失了,再按下 back 才会退出。

4.2.4 碎片和活动之间进行通信

为了方便碎片和活动之间进行通信,FragmentManager 提供了一个类似于 findViewById() 的方法,专门用于从布局文件中获取碎片的实例。

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

这个方法是适用于在布局中通过<fragment>静态添加 Fragment 的情况,如果是动态的,就直接 new Fragment() 了。

那么如何在碎片中调用 Activity 呢?其实每个碎片中都可以通过调用 getActivity() 方法来得到和当前碎片相关联的 Activity 实例。

那么碎片与碎片直接如何通信呢?

思路:首先在一个碎片中可以得到与之关联的 Activity,然后通过这个 Activity 再去获取另外一个碎片实例就可以了。

原文地址:https://www.cnblogs.com/sydmobile/p/12069359.html

时间: 2024-08-29 19:36:13

从0系统学Android--4.1探究碎片的相关文章

从0系统学Android--3.6 RecyclerView

从0系统学Android--更强大的滚动控件---RecyclerView 本系列文章目录:更多精品文章分类 本系列持续更新中.... 参考<第一行代码> 首先说明一点昨天发了一篇关于 ListView 的使用入门文章,得到了大家的一致调侃.我的想法是这样的,虽然现在 ListView 已经被 RecyclerView 替代了,但是本系列作为入门系列,力求内容完整!还是有必要提及一下这么重要的控件的,谁能保证老的项目没有 ListView 呢? 作为入门,一个 Android 开发者不会使用或

从0系统学Android--4.2 Fragment 生命周期

从0系统学Android-- 本系列文章目录:更多精品文章分类 本系列持续更新中.... 初级阶段内容参考<第一行代码> 4.3 碎片的生命周期 碎片也有自己的生命周期,并且和 Activity 的生命周期还很像. 4.3.1 碎片的状态和回调 运行状态 当一个碎片可见,并且它所关联的活动正处在运行状态时,这个碎片也处于运行状态. 暂停状态 当一个 Activity 进入暂停状态时,与它相关联的可见碎片就会进入到暂停状态. 停止状态 当一个活动进入了停止状态时,与他关联的碎片就会进入到停止状态

[Android Pro] Android7.0系统 关于Android获取流量计数TrafficStats.getUidRxBytes(uid)和TrafficStats.getUidTxBytes(uid)返回-1解决方案

reference : http://blog.csdn.net/zhangyong7112/article/details/54574214 最近一个关于流量的项目在Android7.0系统的手机上运行,一直获取不到流量的使用数据,查看源码然后发现TrafficStats.getUidRxBytes(uid)和TrafficStats.getUidTxBytes(uid)一直都是返回的-1, // 获取某个网络UID接收和发送字节的总和 long total = TrafficStats.ge

从0系统学Android--1.2 手把手带你搭建开发环境

要想进行程序开发,首先我们需要搭建开发环境,下面就开始搭建环境. 1.2.1 所需的工具 首先 Android 开发是基于 Java 的,因此你需要掌握简单的 Java 语法.会基础的 Java 语法就可以开始. JDK Java 语言的开发工具包,包含了 Java 的运行环境.工具集合.基础类库等 Android SDK Android 的开发工具包,我们进行 Android 开发离不开这个工具包 Android Studio Android Studio 就是我们的开发软件(写代码用的软件)

从0系统学Android-2.3使用 Intent 在 Activity 之间穿梭

2.3 使用 Intent 在 Activity 之间穿梭 在上一节中我们已经学会了如何创建一个 Activity 了.对于一个应用程序来说,肯定不可能只有一个 Activity.下面就来学习多个 Activity 是专门跳转的. 2.3.1 使用显式 Intent 对于创建 Activity 的过程我们已经很熟悉了,下面快速的创建第二个 Activity.取名 SecondActivity.好了第二个 Activity 已经创建好了,创建好了 Activity 后不要忘了需要在 Android

第8章 Android 4.0系统的下载与编译

第8章  Android 4.0系统的下载与编译 本章首先的准备Android下载与编译环境,内容主要分为:准备Android下载与编译环境.下载源码.编译源码及内核源码.下载Android 4.0及Goldfish源码中包括下载并初始化repo工具.下载Android源码.其他源码下载源.下载模拟器Goldfish内核源码.编译Android及Goldfish内核源码中切换到Android源码目录命令:$cd WORKING_DIRECTORY 执行如下命令,加载编译过程中用到的命令.环境变量

Android开发之深入理解Android 7.0系统权限更改相关文档

摘要: Android 6.0之后的版本增加了运行时权限,应用程序在执行每个需要系统权限的功能时,需要添加权限请求代码(默认权限禁止),否则应用程序无法响应:Android 7.0在Android 6.0的基础上,对系统权限进一步更改,这次的权限更改包括三个方面: APP应用程序的私有文件不再向使用者放宽 Intent组件传递file://URI的方式可能给接收器留下无法访问的路径,触发FileUriExposedException异常,推荐使用FileProvider DownloadMana

Android 5.0系统特性全解析

Android 5.0 Lollipop是今年最为期待的产品升级之一.它将带来全新的设计语言,更多人性化的功能,以及最纯正的Google味道. 最近Google陆续发布的Inbox.新版Gmail和今天公布的新版Google Calendar,都让人认识到Material Design的魅力.到底Android 5.0比之前的版本有多大的变化?来看看Engadget是怎么说的. Material Design 正如Engadget所说,Google计划将在所有的产品当中采用Material De

使用 VirtualBox 虚拟机在电脑上运行 Android 4.0 系统,让电脑瞬间变安卓平板

Ref: http://www.iplaysoft.com/android-v4-ics-for-virtualbox.html 随着?Android?手机的各种软件应用越来越多,很多没有购买的朋友都纷纷表示想要试一试.虽然官方的Android SDK开发包中附带有模拟器,但安装使用上较为复杂,不太适合咱们普通青年,于是我们介绍了一款更易用的BlueStacks 安卓模拟器,不过它也还有一些不爽的地方,譬如只能在 Windows 上运行,或是有时不能连接网络. 所以今天给大家介绍另外一款可以在