Android基础知识 - 碎片

碎片的简单用法

  • 简单事例
  • 在一个活动中添加两个碎片,并让这个两个碎片平分活动空间。
  • 新建一个左侧碎片布局left_fragment.xml,代码如下所示:
<?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"
        android:text="Button"/>

</LinearLayout>
  • 新建一个右侧碎片布局right_fragment.xml,代码如下所示:
<?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"
        android:textSize="20sp"
        android:text="This is a right fragment"/>

</LinearLayout>
  • 新建一个LeftFragment类,继承自Fragment。
package com.example.fragmenttest;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

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;
    }
}
/*
这里仅仅是重写了Fragment的onCreateView()方法,然后在这个方法中通过LayoutInflater的inflate()方法将刚才定义的left_fragment布局动态加载进来,整个方法简单明了。
*/
  • 用同样的方法新建一个RightFragment,代码如下所示:
package com.example.fragmenttest;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

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.right_fragment,container,false);
        return view;
    }
}
  • 修改activity_main中的代码,如下所示:
<?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="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"/>

</LinearLayout>
  • 可以看到,我们使用了\<fragment\>标签在布局中添加碎片,其中指定的大多数属性你都是熟悉的,只不过这里还需要通过android:name属性来显示指明要添加的碎片类名,注意一定要将类的包名也加上。
  • 效果如下所示:
    在这里插入图片描述

动态加载碎片

  • 碎片真正强大之处在于,它可以在程序运行时动态地加载到活动当中。根据具体情况来动态地加载碎片,你就可以将程序界面定制得更加多样化。
  • 在上一节代码的基础上进行完善:
  • 新建another_right_fragment.xml,如下所示:
<?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"
        android:textSize="20sp"
        android:text="This is another right fragment"/>

</LinearLayout>
  • 这个布局文件的代码和right_fragment.xml中的代码基本相同
  • 新建AnotherRightFragment作为另一个右侧碎片,代码如下所示:
package com.example.fragmenttest;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

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.right_fragment,container,false);
        return view;
    }
}
  • 修改activity_main.xml文件,代码如下所示:
<?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="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

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

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

    </FrameLayout>
</LinearLayout>
  • 现在将右侧碎片放在了一个FrameLayout中,FrameLayout是Android中最简单的一种布局,它没有任何的定位方式,所有的控件都会摆在布局的左上角。由于这里仅需要在布局里放入一个碎片,因此非常适合使用FrameLayout。
  • 之后我们将在代码中替换FrameLayout里的内容,从而实现动态添加碎片的功能。修改MainActivity中的代码,如下所示:
package com.example.fragmenttest;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

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

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.button:
                // 待添加的碎片实例
                AnotherRightFragment fragment = new AnotherRightFragment();
                // 获取到FragmentManager,在活动中可以直接调用getSupportFragmentManager()得到
                FragmentManager fragmentManager = getSupportFragmentManager();
                // 开启一个事务,通过调用beginTransaction()开启
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                // 向容器内加入碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例。
                transaction.replace(R.id.right_layout,fragment);
                // 在碎片中模拟返回栈
                transaction.addToBackStack(null);
                // 提交事务,调用commit()方法来完成
                transaction.commit();
                break;
            default:
                break;
        }
    }
}
  • 注意: Fragment、FragmentManager 、FragmentTransaction 都是androidx.fragment.app包中的
  • 注意: getFragmentManager()方法在androidx包中没有了,取代的是getSupportFragmentManager() 方法。
  • 可以看到,首先我们给左侧碎片中的按钮注册了一个点击事件,然后将动态添加碎片的逻辑都放在了点击事件里进行。结合代码可以看出,动态添加碎片主要分为5类:
    • 待添加的碎片实例
    • 获取到FragmentManager,在活动中可以直接调用getSupportFragmentManager()得到
    • 开启一个事务,通过调用beginTransaction()开启
    • 向容器内加入碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例。
    • 提交事务,调用commit()方法来完成
  • 效果图:
    在这里插入图片描述

在碎片中模拟返回栈

  • 尝试下,如果通过点击按钮添加了一个碎片之后,这时按下Back键程序就会直接退出。如果这里我们想模仿类似于返回栈的效果,按下Back键就可以回到上一个碎片,在事务提交之前,直接加一句transaction.addToBackStack(null);

碎片的最佳实践 - 一个简易版的新闻应用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值