Android Fragment的具体使用方式详解

 更新时间:2022年12月01日 16:26:08   作者:z啵唧啵唧  
Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称Activity片段

Fragment的简单用法

  • 在一个Activity中添加两个Fragment,并让这两个Fragment平分Activity空间
  • 新建左侧Fragment的布局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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal"
        android:text="Button"/>
</LinearLayout>

新建右侧Fragment的布局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_horizontal"
        android:textSize="24sp"
        android:text="This is  right fragment"/>
</LinearLayout>
  • 接着新建一个LeftFragment类,让他继承Fragment,这这里会存在两个包下的Fragment提供给我们使用
  • 我们要选择的是AndroidX库中的Fragment,因为它可以让Fragment的特性在所有的Android系统版本中保持一致.
  • 使用AndroidX库中的Fragment并不需要在build.gradle文件中添加额外依赖,只需要在创建新项目的时候勾选Use androidx.* artifacts选项,AS会自动帮助导入必要的AndoidX库
  • LeftFragmeng代码
class LeftFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.left_fragment, container, false)
    }
}
  • 这里重写了Fragment类的onCreateView()方法,在这个方法当中通过LayoutInflater的inflater()方法将定义的left_fragment.xml布局动态加载进来
  • 以同样的方式,创建RightFragment
class RightFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.right_fragment, container, false)
    }
}

在activity_main.xml添加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:orientation="horizontal">
    <fragment
        android:id="@+id/leftFrag"
        android:name="com.zb.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <fragment
        android:id="@+id/rightFrag"
        android:name="com.zb.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

需要注意的是fragment需要通过android:name属性来显示的添加Fragment类名,需要注意的是一定要将类的包名也加上.

动态添加Fragment

  • Fragment真正强大之处在于它可以在程序运行时动态的添加到Activity中,根据具体情况来动态的添加Fragment,使得程序的定制变得多样化.
  • 新建another_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="#FFFF00"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="This is another right fragment"
        android:textSize="24sp" />
</LinearLayout>

新建AnotherRightFragment类用于加载布局

class AnotherRightFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.another_right_fragment, container, false)
    }
}

接下来我们动态的将它添加到Activity中,修改activity_main.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="horizontal">
    <fragment
        android:id="@+id/leftFrag"
        android:name="com.zb.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/rightFrag">
    </FrameLayout>
</LinearLayout>

在代码当中向FrameLayout中添加内容,从而动态的实现Fragment的功能,修改MainActivity当中的代码

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //点击按钮的时候使用AnotherRightFragment
        button.setOnClickListener {
            //创建一个Fragment实例传入replaceFragment()方法进行处理
            replaceFragment(AnotherRightFragment())
        }
        //不点击按钮的时候使用RightFragment,从而实现一个动态的效果
        replaceFragment(RightFragment())
    }
    private fun replaceFragment(fragment: Fragment) {
        //调用getSupportFragmentManager()方法获取fragmentManager
        val fragmentManager = supportFragmentManager
        //fragmentManager调用beginTransaction()开启一个事务
        val transition = fragmentManager.beginTransaction()
        //向容器添加或者Fragment
        transition.replace(R.id.rightLayout, fragment)
        //调用commit()方法进行事务提交
        transition.commit()
    }
}

在Fragment中实现返回栈

  • 实现了向Activity中动态添加了Fragment的功能后,发现通过点击按钮添加了一个Fragment之后这时候按下返回键,程序会直接退出
  • 如果我们想要实现类似于返回栈的效果,按下返回键可以回到上一个Fragment,该如何实现呢?
  • 其实很简单,在FragmentTransation中提供了一个addToBackStack(),可用于将一个事务添加到返回栈当中
  • 修改MainActivity中的代码实现一下这个功能,只需要在replaceFragment()方法当中加入一行代码即可
    private fun replaceFragment(fragment: Fragment) {
        //调用getSupportFragmentManager()方法获取fragmentManager
        val fragmentManager = supportFragmentManager
        //fragmentManager调用beginTransaction()开启一个事务
        val transition = fragmentManager.beginTransaction()
        //向容器添加或者Fragment
        transition.replace(R.id.rightLayout, fragment)
        //给Fragment添加一个返回栈
        //接受一个名字用于描述返回栈的状态,一般传入null
        transition.addToBackStack(null)
        //调用commit()方法进行事务提交
        transition.commit()
    }

Fragment和Activity之间的交互

虽然Fragment是嵌入在Activity中进行显示的,但是它们之间的关系并不是那么亲密

实际上Activity和Fragment是各自存在于一个独立的类当中的,它们之间没有那么明显的方式来直接进行交互

为了方便Fragment和Activity之间进行交互,FragmentManager提供了类似于findViewById()的方法,专门用于从布局文件当中获取Fragment实例.代码如下所示

val fragment = supportFragment.findFragmentById(R.id.leftFrag) as LeftFragment

调用FragmentManager的findFragmentById()方法,可以在Activity中得到响应Fragment的实例,然后就能轻松调用Fragment中的方法了.

另外类似于findiewById(), kotlin-android-extensions 插件也对findFragmentById()方法进行了拓展,允许我们直接使用布局文件中定义的Fragment id名称来自动获取相应的Fragment实例

例如:

val fragment = leftFrag as LeftFragment

在Activity中调用Fragment的相关方法如上操作

在Fragment中调用Activity的方法也是十分的简单

在每个Fragment当中都可以使用getActivity()方法来获得和当前Fragment相关联的Activity实例

例如:

if (activity != null) {
    val mainActivity = activity as MainActivity
}

到此这篇关于Android Fragment的具体使用方式详解的文章就介绍到这了,更多相关Android Fragment内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于Android studio3.6的JNI教程之opencv实例详解

    基于Android studio3.6的JNI教程之opencv实例详解

    这篇文章主要介绍了基于Android studio3.6的JNI教程之opencv实例详解,本文通过实例代码截图的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • Android表格图标库制作

    Android表格图标库制作

    本篇文章主要给大家分享了在Android平台上制作一个含有表格的图标库实例,对此有兴趣的朋友参考下吧。
    2018-01-01
  • Android编程实现全局获取Context及使用Intent传递对象的方法详解

    Android编程实现全局获取Context及使用Intent传递对象的方法详解

    这篇文章主要介绍了Android编程实现全局获取Context及使用Intent传递对象的方法,结合实例形式分析了Android全局Context的获取及Intent传递对象的具体操作方法,需要的朋友可以参考下
    2017-08-08
  • Android App开发中HTTP扩展包OkHttp的入门使用指南

    Android App开发中HTTP扩展包OkHttp的入门使用指南

    OkHttp包为安卓开发中基于HTTP协议的网络编程提供了很大便利,这里我们就来看一下Android App开发中HTTP扩展包OkHttp的入门使用指南:
    2016-07-07
  • Android编程设置全屏的方法实例详解

    Android编程设置全屏的方法实例详解

    这篇文章主要介绍了Android编程设置全屏的方法,结合实例形式较为详细的分析了Android设置全屏的两种常见技巧,非常简单实用,需要的朋友可以参考下
    2015-11-11
  • Android绘图之Paint的使用方法详解

    Android绘图之Paint的使用方法详解

    这篇文章主要给大家介绍了关于Android绘图之Paint使用的相关资料,文中通过示例代码介绍的非常详细,并给大家介绍了DrawText 基线确定的方法,需要的朋友可以参考借鉴,下面随着小编来一些学习学习吧。
    2017-11-11
  • Android中AsyncTask异步任务使用详细实例(一)

    Android中AsyncTask异步任务使用详细实例(一)

    AsyncTask是Android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI主线程,通过本文给大家介绍Android中AsyncTask异步任务使用详细实例(一),需要的朋友参考下
    2016-02-02
  • Android开发基于Drawable实现圆角矩形的方法

    Android开发基于Drawable实现圆角矩形的方法

    这篇文章主要介绍了Android开发基于Drawable实现圆角矩形的方法,结合实例形式分析了Drawable的功能、相关图形绘制函数与使用方法,需要的朋友可以参考下
    2017-10-10
  • Android图片压缩几种方式总结

    Android图片压缩几种方式总结

    这篇文章主要介绍了Android图片压缩几种方式总结的相关资料,需要的朋友可以参考下
    2017-06-06
  • 浅谈Android中视图动画的属性与使用

    浅谈Android中视图动画的属性与使用

    这篇文章给大家简单介绍了Android中视图动画的基本属性以及使用示例,对大家的学习有一定的参考价值,有需要的朋友们下面来一起看看吧。
    2016-09-09

最新评论